2

How can I detect a known image or pattern within an image so that I can replace it with my own text?

Input option 1 (detect aaa and bbb separately):

aaa and bbb are 'slots' where I will place my text

Input option 2 (detect red value and blue value separately): Here I use colored rectangles to mark the spaces to fill in

Output:

what results from the replacement

2
  • What representation do you have of the pattern you are looking for? Nov 1, 2010 at 18:14
  • Do you mean to ask if I can choose what the patterns are? Yes. I will create the source image. The molecule comes from another program, but I will manually label where the numbers will go by any means necessary.
    – ojreadmore
    Nov 1, 2010 at 18:26

1 Answer 1

3

Running code

I'll show you the code and results running in Mathematica using your option 2.

First we read the image

 m = Import@"C:\\imagereplace.png"  

Then we separate the channels RGB

 ColorSeparate[m]

Obtaining

alt text

Next we correlate the red channel image (the one to the right) with a Box Matrix, retaining only the rectangular shape. And transform the result to a B&W image.

Binarize@ImageCorrelate[m1[[3]], BoxMatrix[3]] ;

Obtaining a full size image but containing only the black rectangle.

Now we find the edges of the rectangle (just a loop).

Having the size and coordinates of the rectangles, we create a raster image of the text, corresponding to the detected size, getting:

r1 = Binarize@Rasterize["My Text", RasterSize -> {jmax-jmin + 1, imax-imin + 1}, 
                                    ImageSize -> {jmax-jmin + 1, imax- imin + 1}]

alt text

Now we replace the data block with the new one. Obtaining:

alt text

I'll not do the Blue channel, as it is the same thing.

HTH!!

Note: The Image correlation is the only trick used. The rest is code as usual. Here you can find the basics about correlation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.