5

I'm trying to compare shapes in php. I have a database with a lot of images, in those images there is a shape ( mostly in the center of the image ). Now I want to compare those images to a Shape.

The input is a shape (png) that is exact the same size then the images in the database.

I made a function that takes the color of the shape and turns it in black ( and the background in white)

The input is like this: enter image description here

The original is like this: enter image description here

I can compare every pixel in the image and track how many black pixels are the same, but this will take a lot of CPU and I think it will not work every time.

I also can use an another language like python, but I really wanne fix this in PHP. Does anyone has an idea to do this in a practical way?

The anwser in the question "Compare 2 images in php" is different then I expect. I need to compare a part of an image ( in the example the background in white, but it can be there is some noise in the background )

Community
  • 1
  • 1
S.Visser
  • 4,645
  • 1
  • 22
  • 43
  • possible duplicate of [Compare 2 images in php](http://stackoverflow.com/questions/3270929/compare-2-images-in-php) – cmorrissey Jul 28 '15 at 20:05
  • Not real @cmorrissey, I need to compare a small part of the image, the `ImageMagick ` lib compares the whole.. In my example I show the best result, but it can be that there is some noise in the image. – S.Visser Jul 28 '15 at 20:23
  • there were a few answers there, check out `libpuzzle` https://github.com/jedisct1/libpuzzle – cmorrissey Jul 29 '15 at 14:51
  • Maybe this could be useful: http://compareimages.nikhazy-dizajn.hu/ – NovaLogic Aug 01 '15 at 11:21
  • At some point you will have to compare every pixel. But you can optimize the process by making incrementally accurate comparisons. Could you provide the code used to turn the images black and white and a few sample images ? – spenibus Aug 01 '15 at 12:32

1 Answers1

2

This seems to be doable with the trimImage() function in ImageMagick (-trim flag on the command line). Considering you were already able to convert the image into a black foreground and white background, this function should always do what you want. At this point just compare images as stated in the question you linked.

This is the result of using trim on the command line on the image with padding:

enter image description here


I assume that the function that you made is similar to the algorithm in findimagedupes. If not, you should borrow that idea.

Anonymous
  • 11,740
  • 3
  • 40
  • 50
  • Hi @anonymous. Thank for the answer. I will write a post where I show some code and explain it, but here is it in an nutshell: I'm using `blackThresholdImage()` to make the image black and white. Then I use the `morphology` function to remove the noise in the image. After that I `trim` the image and run the dupes algorithm.. In a quick test ( local ) it checks 100 images in +/- 5 seconds. – S.Visser Aug 04 '15 at 11:26