Getting new objects in the image
<p>i am trying to build a card recognition machine. The thing is the cards will be put on top of a cenario.</p>

<p>That being said i come for help on how can i compare to images, one is the empty background(cenario) and the other is the same cenario with a card on top of it.</p>

<pre><code>import numpy as np
from PIL import Image
import cv2

image1 = cv2.imread("gray_bk.png")
image2 = cv2.imread("gray_novo.png")

cv2.imwrite('LutGrey.png',gray_image)

novo = cv2.subtract(image1,image2) 
cv2.imwrite(file, novo) 
</code></pre>

<p>this is my code so far, the problem with it, is that the return is a black image with the card in it (OK) but the colors in the card are all messed up, how do i execute the same operation without messing the colors ? And what would be the best way to "cut" the card into a new (smaller) image.</p>

<p>If you are working with real images, I suggest the following:</p>

<ul>
<li>read colour images</li>
<li>do some initial presmoothing</li>
<li>substract grayscale images and threshold the result</li>
<li>optionally do some morphological closing</li>
<li>find the biggest region on the thresholded image with labelling</li>
<li>get the bounding rectangle of the card area (for example cv's boundingRect) and extract this area from original colour image Â¶</li>
</ul>

<p>An alternative would be, in general a more robust approach, based on feature detection. Check this out for starter: 
<a href="http://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_feature2d/py_matcher/py_matcher.html" rel="nofollow">http://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_feature2d/py_matcher/py_matcher.html</a>. In this case, all possible card candidates must be known in advance.</p>

<p>If the card was just superimposed on the background image than this might work:</p>

<pre><code># read directly to float 
image1 = cv2.imread("gray_bk.png").astype(np.float) / 255
image2 = cv2.imread("gray_novo.png").astype(np.float) / 255

# substract and threshold
threshold = 0.1
binary_image = np.abs(np.mean(image2,2) - np.mean(image1,2)) &gt; threshold
# get card area
y,x = np.where(binary_image)
# extract card from the image
card = image2[ y.min():y.max(), x.min():x.max(),:] 
</code></pre>

python opencv