Reputation: 71
I have an image which is all black and white and has a certain colored pixels over the black part, which is pink(those colored pixels are all same i.e., they have have same rgb values). I have the numpy ndarray for those pink pixels in the image. And using that ndarray i want to traverse the image , in such a way that for every colored pink pixel i go orthogonal(with yellow i have depicted the direction where i need to travel). I somehow need to find out the width of the black part doing so.
90 degrees to that pixel depiction: [![90 degrees to that pixel depiction][1]][1]
I basically need to note down the sharp change in the pixels while i do so, the moment i see a transition between black pixels to white i need to note down that distance from the point of the colored pixel to the point of transition.
i had written this code which showed me the pixels in an image(rgb) but is not working for my current example here..
for i, j in np.ndindex(img.shape[:-1]):
print(img[i,j])
Upvotes: 1
Views: 101
Reputation: 27547
Here is how, with the input image (image.png
) as:
import cv2
import numpy as np
img = cv2.imread("image.png")
h, w, _ = img.shape
x, y = 300, 160 # Random point on the pink line
strip = img[:, x]
diff = np.diff(np.sum(strip, 1))
# Get y coordinates where...
# white -> black
# black -> pink
# pink -> black
# black -> white
y1, y2, y3, y4 = np.where(diff)[0]
cv2.line(img, (x, y1), (x, y2), (0, 0, 255), 5)
cv2.line(img, (x, y3), (x, y4), (0, 0, 255), 5)
cv2.putText(img, str(y1), (x, y2 - 20), cv2.FONT_HERSHEY_COMPLEX, 1, (0, 255, 0), 2)
cv2.putText(img, str(h - y3), (x, y4 - 20), cv2.FONT_HERSHEY_COMPLEX, 1, (0, 255, 0), 2)
cv2.imshow("result", img)
cv2.waitKey(0)
cv2.destroyAllWindows()
Output:
Upvotes: 0