why from a grayscale image I get values of 255 in pixels that are not white?

357 Views Asked by At

For example in the coordinates of the example vector in the following code block I get for each pixel a value of 255 when in the reference image to various shades of gray

import cv2

#Function to obtain coordinates within the vector
def bresenham(x1, y1, x2, y2):
    # Inicializar variables
    dx = abs(x2 - x1)
    dy = abs(y2 - y1)
    x, y = x1, y1
    sx = -1 if x1 > x2 else 1
    sy = -1 if y1 > y2 else 1
    error = dx - dy

    # Iterate over the points of the vector
    coords = []
    while True:
        coords.append((x, y))
        if x == x2 and y == y2:
            break
        e2 = 2 * error
        if e2 > -dy:
            error -= dy
            x += sx
        if e2 < dx:
            error += dx
            y += sy
    return coords

#Create images
originalImage = cv2.imread("image.jpg",3)
gray = cv2.cvtColor(originalImage, cv2.COLOR_BGR2GRAY)

# Vector coordinates
x1, y1 = 300, 120
x2, y2 = 400, 200

#Obtain the coordinates inside the vector
coords = bresenham(x1, y1, x2, y2)

# obtain the value of each pixel 
for i in range(len(coords)-1):
    imagex,imagey=coords[i]
    pixel = gray[imagex,imagey]
    print(imagex,imagey)
    print(pixel)

# print(coords)
# cv2.line(gray, (x1, y1), (x2, y2), (0, 0, 0), 1)
cv2.namedWindow("black", cv2.WINDOW_NORMAL)
cv2.imshow("black", gray)

cv2.waitKey()
cv2.destroyAllWindows()

Even drawing a line to corroborate before the pixel check or after to see that you are trying to acquire the correct values.

I attach the reference image.enter image description here

0

There are 0 best solutions below