OpenCV-Python I just wrote six lines of code and a shocking BUG occurred

Click on " Xiaobai Learning Vision " above, and choose to add " star " or " top "

Heavy dry goods, delivered as soon as possible0264888adad634d4db007e88cdfac0fc.png

Introduction

I have been developing with PyQT and python-opencv recently, and I just need a fixed threshold for binarization, so I wrote down the following code snippet:

image[image > t] = maxval
image[image <= t] = 0

I gave myself full marks for this wave of routine operations. Later, others told me that I could reverse the segmentation according to the threshold, that is, if the value of T is greater than the threshold, it is assigned to 0, and the value of T less than the threshold is assigned to maxval. So, I added an if else operation to it. The code is as follows :

if bin_type == 0:
    image[image > t] = maxval
    image[image <= t] = 0
else:
    image[image > t] = 0
    image[image <= t] = 255

I feel that this code is written with full marks, so when I debug, I input a grayscale image. As long as I select bin_type=0, the binarization is always performed correctly, and as long as I input bin_type=1, it is all white. After I tested a few images, I felt a little suspicious of life. The code with such good logic was not executed correctly! 

1. The truth breaks me down

This question was so depressing at the time, I felt that my IQ had a problem! So I manually wrote the following code:

test = np.zeros((8, 8), dtype=np.uint8)
for i in range(8):
    test[i, 0] = i * 32
    test[i, 1] = i * 32
    test[i, 2] = i * 32
    test[i, 3] = i * 32
    test[i, 4] = i * 32
    test[i, 5] = i * 32
    test[i, 6] = i * 32
    test[i, 7] = i * 32
print(test)

test[test > 122.2335] = 0
print("output1\n",test)
test[test <= 122.2335] = 255
print("output2\n", test)

Print out the result:

a6b56f983f25ebf7cd9a6e46b7416468.png

I finally solved the case!

It turns out that the first time I assigned image[image>T] = 0, there was no more than T, and then I executed image[image<T]=255, and it turned out to be all 255, of course, it was all white. IQ has been crushed! Later, I remembered that a long time ago, I downloaded someone else's project from github, and his code had similar writing. I remember that when I debugged and found that this code did not work, because the project code was very long at the time, I saved it. The image was found to be incorrect, so I replaced it with the cv.threshod function of opencv-python! I'm dizzy!

Download 1: OpenCV-Contrib extension module Chinese version tutorial

Reply in the background of the " Xiaobai Learning Vision " official account: Chinese tutorial on extension module , you can download the first Chinese version of the OpenCV extension module tutorial on the whole network, covering extension module installation, SFM algorithm, stereo vision, target tracking, biological vision, super Resolution processing and more than twenty chapters.

Download 2: Python Visual Combat Project 52 Lectures

Reply in the background of the " Xiaobai Learning Vision " public account: Python visual combat project , you can download including image segmentation, mask detection, lane line detection, vehicle counting, adding eyeliner, license plate recognition, character recognition, emotion detection, text content extraction, Facial recognition and other 31 visual combat projects to help fast school computer vision.

Download 3: OpenCV practical project 20 lectures

Reply in the background of the " Xiaobai Learning Vision " official account: OpenCV practical project 20 lectures , you can download 20 practical projects based on OpenCV to achieve advanced OpenCV learning.

exchange group

Welcome to join the public account reader group to communicate with your peers. Currently, there are WeChat groups such as SLAM, 3D vision, sensors, autonomous driving, computational photography, detection, segmentation, recognition, medical imaging, GAN, and algorithm competitions (will be gradually subdivided in the future), Please scan the WeChat account below to join the group, note: "nickname + school/company + research direction", for example: "Zhang San + Shanghai Jiaotong University + Visual SLAM". Please remark according to the format, otherwise it will not be approved. After the addition is successful, you will be invited to enter the relevant WeChat group according to the research direction. Please do not send advertisements in the group, otherwise you will be invited out of the group, thank you for your understanding~

4bef6993392051a43f429180b8b5f495.png

81d070cad3b1e635c841f7ba0a17381c.png

Guess you like

Origin blog.csdn.net/qq_42722197/article/details/123675844