Crop image using black/white mask

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
victorzx
Posts: 10
Joined: 2018-07-23T22:51:52-07:00
Authentication code: 1152

Crop image using black/white mask

Post 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
victorzx
Posts: 10
Joined: 2018-07-23T22:51:52-07:00
Authentication code: 1152

Re: Crop image using black/white mask

Post 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
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Crop image using black/white mask

Post 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.
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Crop image using black/white mask

Post 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.
victorzx
Posts: 10
Joined: 2018-07-23T22:51:52-07:00
Authentication code: 1152

Re: Crop image using black/white mask

Post 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
victorzx
Posts: 10
Joined: 2018-07-23T22:51:52-07:00
Authentication code: 1152

Re: Crop image using black/white mask

Post by victorzx »

So, png is not an option because is more bigger in size than jpg
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Crop image using black/white mask

Post 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.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Crop image using black/white mask

Post 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
victorzx
Posts: 10
Joined: 2018-07-23T22:51:52-07:00
Authentication code: 1152

Re: Crop image using black/white mask

Post by victorzx »

Sorry about my mistake, it for ubuntu deskptop not for win10, this code wil work with linux?
Last edited by victorzx on 2018-07-24T23:11:39-07:00, edited 1 time in total.
victorzx
Posts: 10
Joined: 2018-07-23T22:51:52-07:00
Authentication code: 1152

Re: Crop image using black/white mask

Post by victorzx »

??
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Crop image using black/white mask

Post 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
victorzx
Posts: 10
Joined: 2018-07-23T22:51:52-07:00
Authentication code: 1152

Re: Crop image using black/white mask

Post 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?
victorzx
Posts: 10
Joined: 2018-07-23T22:51:52-07:00
Authentication code: 1152

Re: Crop image using black/white mask

Post by victorzx »

the problem is connected-component
victorzx
Posts: 10
Joined: 2018-07-23T22:51:52-07:00
Authentication code: 1152

Re: Crop image using black/white mask

Post 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
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Crop image using black/white mask

Post 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.
Post Reply