Page 1 of 1

Crop image using black/white mask

Posted: 2018-07-23T22:59:35-07:00
by victorzx
Hello

I have 2 image, image with some element(pinc.jpg) and another only background(bg.jph), I want to use background subtraction and then aaplly a crop using mask

i used the following command

composite pic5.jpg pic5_2.jpg -compose Difference diff_prueba.jpg
convert diff_prueba.jpg -threshold 1% prueba_mask.jpg

convert pic.jpg prueba_mask,jpg -compose darken -composite -trim out.png

And this is the result
https://ibb.co/bNq0R8

But I want only to show de foreground image without that black backgrooubd unsing jpg not png. so i think to resize the image the same size of mask

Re: Crop image using black/white mask

Posted: 2018-07-23T23:11:42-07:00
by victorzx
Excuse me, I want to show only foreground objet that is the result of background sbutracion so I need to crop only ofregrooubd objects

Re: Crop image using black/white mask

Posted: 2018-07-24T04:58:10-07:00
by snibgo
I don't understand what you want. Please link to input images, and a desired output image.

Also say what version of IM you use, on what platform.

Re: Crop image using black/white mask

Posted: 2018-07-24T09:24:12-07:00
by fmw42
I think you want to make the whole background black and then do a -trim resulting in the bounding box around the object. If so, then you need to clean up the background, perhaps using connected components to remove objects smaller than the main object.

Perhaps you want to make the background transparent to only show the object. But JPG does not support transparency, so the result would still be a black image unless you save as PNG.

As snibgo has said, please supply the two input images and clarify further what you want as an output. Perhaps show the output as desired or draw on your output to show what you only want to keep.

Re: Crop image using black/white mask

Posted: 2018-07-24T19:09:29-07:00
by victorzx
This is original photo
https://ibb.co/eZBig8
And this is a bg
https://ibb.co/d8mzM8

So I want to trim the result image in order to get only foreground image.
for example I want this result

https://ibb.co/bSh9oo

IM 7.0.8 Windows 10

Re: Crop image using black/white mask

Posted: 2018-07-24T19:11:01-07:00
by victorzx
So, png is not an option because is more bigger in size than jpg

Re: Crop image using black/white mask

Posted: 2018-07-24T20:35:31-07:00
by fmw42
Please clarify about what background color you want when removing the rest of the image? JPG does not support transparency. So if you need the background transparent the only choices are PNG8 (8-bit color not 24-bit color) or GIF. Both support binary transparency. If you need JPG, then you would have to specify some opaque background color.

Re: Crop image using black/white mask

Posted: 2018-07-24T21:03:57-07:00
by fmw42
Try these two. The first keeps the full image size with black background. The second trims to the minimum bounding box. Windows syntax. (If using a bat file, then double the % to %%)

Code: Select all

convert -respect-parenthesis foreground.jpg ^
( -clone 0 background.jpg -colorspace gray -compose difference -composite -threshold 5% -type bilevel -compose over ^
-define connected-components:area-threshold=10000 ^
-define connected-components:mean-color=true ^
-connected-components 4 ) ^
-alpha off -compose copy_opacity -composite ^
-background black -alpha background -alpha off ^
result1.jpg

Code: Select all

convert -respect-parenthesis foreground.jpg ^
( -clone 0 background.jpg -colorspace gray -compose difference -composite -threshold 5% -type bilevel -compose over ^
-define connected-components:area-threshold=10000 ^
-define connected-components:mean-color=true ^
-connected-components 4 ) ^
-alpha off -compose copy_opacity -composite ^
-background black -alpha background -alpha off ^
-trim result2.jpg

Re: Crop image using black/white mask

Posted: 2018-07-24T22:44:53-07:00
by victorzx
Sorry about my mistake, it for ubuntu deskptop not for win10, this code wil work with linux?

Re: Crop image using black/white mask

Posted: 2018-07-24T23:00:06-07:00
by victorzx
??

Re: Crop image using black/white mask

Posted: 2018-07-25T08:54:27-07:00
by fmw42
Unix syntax

Code: Select all

convert -respect-parenthesis foreground.jpg \
\( -clone 0 background.jpg -colorspace gray -compose difference -composite -threshold 5% -type bilevel -compose over \
-define connected-components:area-threshold=10000 \
-define connected-components:mean-color=true \
-connected-components 4 +write tmp.gif \) \
-alpha off -compose copy_opacity -composite \
-background black -alpha background -alpha off \
result1.jpg

Code: Select all

convert -respect-parenthesis foreground.jpg \
\( -clone 0 background.jpg -colorspace gray -compose difference -composite -threshold 5% -type bilevel -compose over \
-define connected-components:area-threshold=10000 \
-define connected-components:mean-color=true \
-connected-components 4 +write tmp.gif \) \
-alpha off -compose copy_opacity -composite \
-background black -alpha background -alpha off \
-trim result2.jpg

Re: Crop image using black/white mask

Posted: 2018-07-25T17:28:45-07:00
by victorzx
oooh. thats work with IM 7.0 but not in 6.7 o older, what is the problem if I want to do that in IM 6.7?

Re: Crop image using black/white mask

Posted: 2018-07-25T17:31:52-07:00
by victorzx
the problem is connected-component

Re: Crop image using black/white mask

Posted: 2018-07-25T17:49:50-07:00
by victorzx
I have a last question, what is the alternative if in my IM version I dont have connected-component? Because I have two linux that one have IM 7.0 and other only I can download IM 6.7 or older version but not earlier

Re: Crop image using black/white mask

Posted: 2018-07-25T18:51:29-07:00
by fmw42
IM 6.7 is too old. You would need to upgrade to at least 6.8.9.10. There is no substitute for it in ImageMagick, if you have too old a version. You could try OpenCV, which has some similar functionality.

If on Linux, then check the date associated with 6.7.x.x. That will show the latest patch. If that date is too old, check with your Linux distro manager to see if there is an upgrade or more current patch.