Extracting sprites/images from a bigger image

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?".
jmspaggi
Posts: 25
Joined: 2020-02-09T15:15:17-07:00
Authentication code: 1152

Re: Extracting sprites/images from a bigger image

Post by jmspaggi »

Ha, that looks pretty nice. I'm totally fine with losing a few pixels on the corners. I'm not going to try to match the forms/sides, but I want to do Image Recognition instead. But I want to focus on the clean piece, not the dust.

So just to make sure I get the steps correct.
I do a binary operation with this image and my initial one to turn all what is white here, transparent on the other one.
Then I do objects detection to get all the boxes, and last I just cut those objects on the image with transparency.

Souds like a plan! I will try that after work, and post the results here.

BTW, the white vertical line on the picture is coming from the scanner. I'm not sure what that is. But I might be able to re-scan without that line...

Thanks again!

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

Re: Extracting sprites/images from a bigger image

Post by fmw42 »

Try this. I had to use IM 7 to get it to work (7.0.9.22) because connected components reported an error. But it may work fine in other versions or platforms. I will look into the bug.

I first do a flood fill to make a binary image. Then I use CCL to remove small regions (so I can use as small a morphogoly kernel as possible for this image). The I do a morphology close. Then I do CCL again just to get the bounding boxes of the remaining regions and save the mask image. Then I loop over the bounding boxes and extract each puzzle piece and make the background transparent with antialiasing using the same cropped mask.

Code: Select all

bboxArr=(`magick input_white_01.tif \
-bordercolor white -border 1 \
-fuzz 1.5% -fill white -draw "color 0,0 floodfill" -alpha off \
-fill black +opaque white -negate -type bilevel \
-define connected-components:mean-color=true \
-define connected-components:area-threshold=10000 \
-connected-components 4 \
-morphology close disk:7 \
-define connected-components:verbose=true \
-define connected-components:mean-color=true \
-connected-components 4 mask.gif | grep "gray(255)" | awk '{print $2}'`)
num=${#bboxArr[*]}
echo $num
magick input_white_01.tif input_white_01.mpc
for ((i=0; i<num; i++)); do
echo $i
ii=`printf "%02d" $i`
bbox=${bboxArr[$i]}
magick \
\( input_white_01.mpc -crop $bbox +repage \) \
\( mask.gif -crop $bbox +repage -blur 0x2 -level 50x100% \) \
-alpha off -compose copy_opacity -composite input_white_01_part_$ii.png
done

jmspaggi
Posts: 25
Joined: 2020-02-09T15:15:17-07:00
Authentication code: 1152

Re: Extracting sprites/images from a bigger image

Post by jmspaggi »

Wow! Seems that I can stop working on it! Seems to be working very well! I tried to reduce the fuzz parameter, to "eat" less of the white piece, but I can see the line that we talked about before. I will check if there is something on the scanner, or on the way I installed the pieces. I will also try another scanner, just in case.

Now I need to read about morphology parameter...

Thanks a lot! That will totally work for what I need to do, and will save me a lot of time!

Post Reply