I am a newbie to Python.
And I want to access Mat (a threshold binary image) element from opencv and create the a histogram according to the x-axis, and so I can do some image segmentation vertically.
What I have done is to preprocess the image and enumerate twice as following.
def crack(src):
#src is a binary image
src = cv2.resize(src, (0,0), fx=6, fy=6)
thresh = preprocessing(src)
#create empty histogram
print(thresh.shape)
height, width = thresh.shape
size = height ,255, 3
hist = np.zeros(size, dtype=np.uint8)
#enumerate elements
for y, x in enumerate(thresh):
val = 0
for x, pix in enumerate(x):
val = val+ pix/255
print y,x, val
cv2.rectangle(hist, (y, 255-val), (y+val, 255-val+1), (0, 255, 0), 1)
cv2.imshow("thresh", thresh)
cv2.imshow("hist", hist)
And if I directly enumerate the threshold Mat like
for y, x in enumerate(thresh):
I can only enumerate from the outer y axis first then enumerate the x axis. So how can I do this reversely? My aim is to get the image like this:
image1 http://7xsnr6.com2.z0.glb.clouddn.com/QQ%E5%9B%BE%E7%89%8720160509184009.jpg
Image references:
Jeff Yan, A Low-cost Attack on a Microsoft CAPTCHA
If you want the number of black pixels for each x
position, following your code this should work:
# Enumerate elements
for x in range(width):
val = 0
for y in range(height):
val += (255-thresh[y, x])/255
print y, x, val
cv2.line(thresh, (x, height-val-1), (x, height-1), (0, 255, 0), 1)
So basically you are looping over the x
axis and, for each of its position, finding the amount of black pixels over the y
axis, which is stored in the variable val
. Finally you draw a line that has a height equal to val
pixels at the x
position you are looping that starts at the bottom of the image.
Hope this helped!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.