Page 1 of 1

Center an object in a given picture

Posted: 2014-07-04T00:17:41-07:00
by farokojil
I have a task to write a PHP script that makes objects in the center of their pictures, For example Image Image

Some of the pictures are horizontally oriented and some are vertically oriented based on that and the size of the white space I have to Crop/Add white space to the original picture.

The first method I had is by detecting the borders of the object within the picture but some picture doesn't have clear white background e.g: Image

This Picture has extra white space on the borders and has gray gradient as the background which makes it harder to detect the object's borders, So I tried to apply The Sobel operator by Imagemagick

Code: Select all

exec("convert 1.jpg -define convolve:scale='50%!' -bias 50%  -morphology Convolve Sobel -solarize 50% -level 50,0%  ssc1.jpg");
The result was ok

Image

And Now I have to find the borders in the filtered image, And the question is

What's the best way to find the coordinates of the borders (the output should be X1,X2,Y1,Y2)?

I have read some similar problems like this one that converts the image to text and remove white (black in my case) pixels but I'm not sure what's the best approach to solve this (I'm newbie in Image Processing).

Re: Center an object in a given picture

Posted: 2014-07-04T09:34:59-07:00
by fmw42
(-fuzz XX%) -trim will crop the image to the bounding box. But there is a string format that will get those coordinates for you without cropping.

convert image -fuzz XX% -trim -format "%@" info:

see http://www.imagemagick.org/script/escape.php

But if you just need to center some image and remove the excess background. You can trim and then extend to the size you want.

convert image -fuzz XX% -trim +repage -background somecolor -gravity center -extent WxH resultimage

The fuzz value allows you to trim if the background is not not perfectly constant.

The extent will pad the image out the the desired WxH with the the background color and the image centered (via -gravity center)

Re: Center an object in a given picture

Posted: 2014-07-07T18:29:53-07:00
by anthony