Recently, OpenCV PYTHon3 was used to develop the application of scene recognition. The problem was that the image could not be accurately displayed after mathematical logic operation. The problem code is as follows:

out = 1*((img[:,:,2]>img[:,:,1])&(img[:,:,1]>img[:,:,0]))
print(out.shape,np.sum(out))
while (True):
    cv2.imshow("myWin", (out*255))
    if(cv2.waitKey(1000//12) &  0xff == ord("q")):
        #cv2.imwrite("test/chess_deal.png", out*255)
        break
cv2.destroyAllWindows()
Copy the code

The shape and data of the output image are fine, but they are not displayed correctly. Finally, we found the problem with a statement:

print(out.dtype)
Copy the code

The output is:

int64
Copy the code

As OpenCV needs uint8 type to process data, the image is converted to INT64 for mathematical logic operation. If OpenCV wants to display correctly, data type conversion is needed:

out_deal = out.astype(np.uint8)
Copy the code

The complete code is as follows (OpenCV do flame dynamic detection device) :

Import cv2 import numpy as NP """ There are two types of photoreceptors on the retina of the human eye: cones and rods. The cone is located mainly in the middle part of the retina, called the fovea, and is highly sensitive to color, called daylight or bright vision. The rods have a large distribution area and are used to give a general general image of the field of vision. They have no sense of color and are sensitive to low illumination, known as low-light vision or dark vision. Since the cone is sensitive to red, green and blue light, the color model commonly used for human eyes is RGB. Generally speaking, no matter the pictures or videos downloaded from the Internet, or the video from the camera, are RGB models. Therefore, we download a flame image from the Internet, and can use RGB color criterion to extract regions without any color model transformation. For a common flame, its red AND green components will be large, AND the green component will be larger than the blue component, so the simple criterion we set is: R > R_avg AND G > G_avg AND R > G > B where, R_avg is the mean value of the red component. In OpenCV1.0 implementation is very simple, posing as a first code below -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - the author: electromagnetic gun source: X CSDN, https://blog.csdn.net/qq_27569955/article/details/51502824 copyright statement: this article original articles for bloggers, reproduced please attach link to blog! """ print(cv2.__version__) img = cv2.imread("test/flame1.jpg") line, cols, chl = img.shape img_avg = [] for i in range(chl): img_avg.append(np.average(img[:,:,i])) overAvg = [] for i in range(chl): overAvg.append(img[:,:,i]>img_avg[i]) #out = (overAvg[1] and overAvg[0]) and ((img[:,:,2]>img[:,:,1]) and (img[:,:,1]>img[:,:,0])) out = 1*((overAvg[2]&overAvg[1])&((img[:,:,2]>img[:,:,1])&(img[:,:,1]>img[:,:,0]))) out = out print(np.sum(out)/(line * cols)*100,out.shape) print(out.dtype) print((out*255).astype(np.uint8).dtype) while (True): cv2.imshow("myWin", (out*255).astype(np.uint8)) if(cv2.waitKey(1000//12) & 0xff == ord("q")): #cv2.imwrite("test/chess_deal.png", (out*255).astype(np.uint8)) break cv2.destroyAllWindows()Copy the code