Region Extracting and Image Registration

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
Wanderer
Posts: 12
Joined: 2014-05-04T14:45:11-07:00
Authentication code: 6789

Region Extracting and Image Registration

Post by Wanderer »

Hello ,

I am currently trying to work out image registration with imagemagick. I want to try out a couple of things

1. Extracting a image of the product from the bigger image named Image1.jpg.
2. Doing image registration of Image2.jpg with the extracted smaller image in step 1. I guess that would also need cropping the Imag2.jpg first and then doing alignment with extracted image from Image1.jpg.

3. Then comparing the images extracted in step 1 and step 2 of product and comparing.

I know the compare command and crop in imagemagick however how to use these commands in these series of steps looks challenging. Please help.

I am currently working on windows using version 6.8.9-Q16.

The images can be found at the below url.

http://s10.postimg.org/qi6rewawp/Image1.jpg
http://s28.postimg.org/sqzb7d159/Image2.jpg
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Region Extracting and Image Registration

Post by snibgo »

For the challenging series of steps, see my response to the very recent thread viewtopic.php?f=1&t=25539

For the alignment (steps 5 and 6 in my response), you should also look at "-distort".
snibgo's IM pages: im.snibgo.com
Wanderer
Posts: 12
Joined: 2014-05-04T14:45:11-07:00
Authentication code: 6789

Re: Region Extracting and Image Registration

Post by Wanderer »

Hi Snibgo ,

I went through your answer. I believe some learning needs to be done before I can actually do all of it myself.

One quick question though.

Suppose I have a reference image and bigger image which contains the reference image part(probably alignment different). If I put some color border around the image that I want to crop and then crop it and then do the image registration with the reference image.

So as per your answer I understood that I would need a script for it. Can that script be generic if I want to repeat this activity with a series of images(reference images and images that need to be aligned with reference taking into consideration that I keep the same color border in the image that needs to be cropped and then compared with reference image. Or would I need to write a new script for each of this..
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Region Extracting and Image Registration

Post by snibgo »

Sorry, I don't understand your point about adding a color border. What would this be added to? The large image, the small image or the crops used for subimage searches? Why?

If your small image has an added border, it would be sensible to trim it off before doing any searching. If you don't, then the script might search for this border within the large image.

In general, scripts are general purpose. But they always have limitations.
snibgo's IM pages: im.snibgo.com
Wanderer
Posts: 12
Joined: 2014-05-04T14:45:11-07:00
Authentication code: 6789

Re: Region Extracting and Image Registration

Post by Wanderer »

Actually I am going to use this concept in my project. So trying to find out whether it is possible or not.

Answering your question the idea of border was on some portion of the larger image so that I can determine what region from the larger image I need to crop which is the same as the reference image(not considering alignment) and then consider them for image registration. I think the word border used by me was not apt and that might have got you thinking. What I meant was just outlining the region to crop(inside larger image) based on some color so that the logic remains generic for other image cropping as well.
Wanderer
Posts: 12
Joined: 2014-05-04T14:45:11-07:00
Authentication code: 6789

Re: Region Extracting and Image Registration

Post by Wanderer »

Can you please help me with the first part of extracting the image of the box from the main image. I couldn't find that part in the solution you gave in the link.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Region Extracting and Image Registration

Post by snibgo »

There are many ways of doing this. One uses a "difference between two blurs". See my "Details, details" page. I find details in the image, as white points on a black background. Then find trim coordinates, saved into environment variable. Use that to crop the source image.

Windows BAT syntax.

Code: Select all

for /F "usebackq" %%L in (`%IM%convert ^
  Image1.jpg -blur 0x1 ^
  ^( +clone -blur 0x1 ^) ^
  -compose Difference -composite ^
  -grayscale RMS ^
  -auto-level ^
  -fuzz 30%% -fill Black -opaque Black ^
  -fuzz 0 -fill White +opaque Black ^
  -format "%%@" ^
  info:`) do set CROP=%%L

echo %CROP%

%IM%convert ^
  Image1.jpg ^
  -crop %CROP% ^
  b.png
snibgo's IM pages: im.snibgo.com
Wanderer
Posts: 12
Joined: 2014-05-04T14:45:11-07:00
Authentication code: 6789

Re: Region Extracting and Image Registration

Post by Wanderer »

Thanks for you help Snibgo ,

I ran you .bat file however it doesn't give me exact image of the box as the output(i.e some of the image is clipped and some is unwanted) though your script is quite useful. My requirement is to get the exact box image. I don't care if I need to outline the box with some color for identification. Is there some way by which that can be achieved?

One more thing I wanted to know if that part is possible.. I would want to migrate this functionality to java as my project would be in java.. So using Jmagick or im4java is it possible to replicate the same using java interface of imagemagick
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Region Extracting and Image Registration

Post by snibgo »

Sorry, I've spent all the time I can on this problem.

I know nothing about Java.
snibgo's IM pages: im.snibgo.com
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Region Extracting and Image Registration

Post by snibgo »

Here is a Windows 8.1 script that translates, scales and rotates Image1.jpg to match Image2.jpg. The result is i1.png.

Image

As a free bonus, is also does the inverse: it translates, scales and rotates Image2.jpg to match Image1.jpg. The result is i2.png.

Image

It uses only two points, towards the left of the box, so is inaccurate elsewhere. With more fiddling, a greater number of points might match. It could be followed by an area-matching technique to get exact transformations.

Code: Select all

setlocal enabledelayedexpansion

%IM%convert ^
  Image1.jpg ^
  -canny 0x4+10%%+30%% ^
  -grayscale RMS ^
  -blur 0x10 ^
  -auto-level ^
  i1.png

%IM%convert ^
  Image2.jpg ^
  -canny 0x4+10%%+30%% ^
  -grayscale RMS ^
  -blur 0x10 ^
  -auto-level ^
  i2.png


set nlDEBUG=1

call %PICTBAT%nLightest i1.png 2 20 >i1coords.lis
call %PICTBAT%nLightest i2.png 2 20 >i2coords.lis

set CoordNum=0
for /F "tokens=*" %%A in (i1coords.lis) do (
  set i1coord_!CoordNum!=%%A
  set /A CoordNum+=1
  echo CoordNum=!CoordNum!
)

set CoordNum=0
for /F "tokens=*" %%A in (i2coords.lis) do (
  set i2coord_!CoordNum!=%%A
  set /A CoordNum+=1
  echo CoordNum=!CoordNum!
)

%IM%convert ^
  Image1.jpg ^
  -virtual-pixel Black ^
  -distort Affine "%i1coord_0% %i2coord_0% %i1coord_1% %i2coord_1%" ^
  i1.png

%IM%convert ^
  Image2.jpg ^
  -virtual-pixel Black ^
  -distort Affine "%i2coord_0% %i1coord_0% %i2coord_1% %i1coord_1%" ^
  i2.png


rem Make copies for the web.
%IM%convert i1.png -resize 300 i1rot.jpg
%IM%convert i2.png -resize 300 i2rot.jpg
The script uses a blurred Canny edge detection, followed by nLightest.bat to find the two most detailed points. See my page "Details, details".
snibgo's IM pages: im.snibgo.com
Post Reply