Questions regarding Python PIL and cv...

Asked 2 weeks ago, Updated 2 weeks ago, 2 views

First, if I were to show you the result image, this is what it looks like this.

And the result I wanted is to square the orange area.

I measured the pixel coordinate size of the picture and turned x, y as a double for statement, and if the color of the point corresponds to [±10,±10,±10] in the RGB color value of "orange", the coordinates of the point are stored in tmp, compared to the existing path value (coordinates with a default of 0, 0). But that's how it looks. I don't think we're changing coordinates at 0, but I don't know why.

The following is part of the code I wrote.

# Setting the color range
high_color = [find_color[0]+10, find_color[1]+10, find_color[2]+10]
low_color = [find_color[0]-10, find_color[1]-10, find_color[2]-10]

if low_color[1] < 0:
    low_color[1] = 0
    print("Adjusting low_color value...", low_color[1])

print("High color is "", high_color;" Low color is ", low_color;")

# Color recognition coordinate range storage value
axis_x = [0, 0]
axis_y = [0, 0]
print("coordinate_x range is "", axis_x[0], axis_x[1])
print("coordinate_y range is "", axis_y[0], axis_y[1])

# For : Repeat the entire image x and y coordinates ex) (1, 1), (1, 2), (1, 3)...
for x in range(im_x):
    for y in range(im_y):
        # Check the pixel values for the coordinates
        pix_RGB = pix[x][y]
        #print("The pixel value of the current check coordinates is "", pix_RGB")

        # if : If the RGB value of the specified pixel is between the color range values,
        if pix_RGB[0] > low_color[0] and pix_RGB[1] > low_color[1] and pix_RGB[2] > low_color[2] and pix_RGB[0] < high_color[0] and pix_RGB[1] < high_color[1] and pix_RGB[2] < high_color[2]:
            tmp_x = x
            tmp_y = y
            #print("tmp_x, y is", tmp_x, tmp_y)

            # Store tmp_x and y values in range values
            if tmp_x < axis_x[0]:
                axis_x[0] = tmp_x
                print("The minimum value of x has changed!")

            elif tmp_x > axis_x[1]:
                axis_x[1] = tmp_x
                print("The maximum value of x has changed!")

            elif tmp_y < axis_y[0]:
                axis_y[0] = tmp_y
                print("The minimum value of y has changed!")

            elif tmp_y > axis_y[1]:
                axis_y[1] = tmp_y
                print("The maximum value of x has changed!")

print("pixel_RGB value is "", pix_RGB)
print("coordinate_x range is "", axis_x[0], axis_x[1])
print("coordinate_y range is "", axis_y[0], axis_y[1])

imageRectangle = image.copy()
cv2.rectangle(imageRectangle, 
            (axis_x[0],axis_x[0]),
            (axis_x[1],axis_x[1]),
            (0,0,255),
            thickness=5, 
            lineType=cv2.LINE_AA)  
cv2.imshow("image", imageRectangle) 

I haven't seen it for a long time, but I don't know. Does anyone know what's wrong?

cv2 python pil image

2022-09-20 14:38

1 Answers

Thank you for your advice, beginner!

To give a self-answer, the problem was that the default values of Axis_x and y were randomly set to zero, which caused the maximum value to be easily found but the minimum value could not be changed.

The method I worked out was to set the default start coordinate value based on the center value of the entire coordinate, not 0, 0.

However, if the size of the object is formed near the center value, it is normal to identify it, but on the contrary, if the position of the object is biased from the center and the minimum starting area is larger than the center, the recognition is not normal.

So to solve this problem, it seems neat to convert the coordinate values in a range color into a list, and then turn this storage value into a for statement and set the smallest x and y values to the lowest point, and the largest x and y values to the highest point.

I hope there will be no more problems

//

2021-11-16 If the default start value is set to center value, the problem has occurred again that the position of the square area does not change to more than center.

To solve this problem, setting the minimum point of the default value to the image maximum area value and specifying the maximum point as (0, 0) resolved!

The code is as follows.

# Final coordinate storage location, default is coordinate end value / maximum value is 0
axis_x = [im_x, 0]
axis_y = [im_y, 0]

This setting ensures that the maximum point in the area corresponding to the color is unconditionally larger than zero, and thus optimizes, and the minimum point is also unconditionally smaller than the maximum point.


2022-09-20 14:38

If you have any answers or tips


© 2022 pinfo. All rights reserved.