OpenCV Python on RPi, can't seem to iterate through image width [closed]
Hi all, Expect to see me here a bit in the next few weeks, working on my university honours project. I'm rewriting a C++ script using OpenCV that I made a year ago but this time in Python. import cv2 as cv import numpy as np from matplotlib import pyplot as plt image = cv.imread('/home/pi/Desktop/ScannerDev/TestImage.png', cv.IMREAD_GRAYSCALE)
#ret, discrImage = cv.threshold(image,127,255,0)
def imageToPoints (image):
discrImage = cv.threshold (image, 127, 255, 0)
# Add a line of white values along the top of the image.
# This ensure that the binary outside contour generation works.
# It is removed later before points are written to point cloud.
for x in range (discrImage.cols):
# Set image x,0 = 255
discrImage[x,0] = [255,255,255]
return discrImage
while (1):
discrImage = imageToPoints (image)
cv.imshow ('original', image)
cv.imshow ('thresholded', discrImage)
k = cv.waitKey(5) & 0xFF
if k == 27:
break
cv.destroyAllWindows()
At the moment I'm working back up from examples online. It seems to all work fine up to this section: # Add a line of white values along the top of the image. # This ensure that the binary outside contour generation works. # It is removed later before points are written to point cloud. for x in range (discrImage.cols): # Set image x,0 = 255 discrImage[x,0] = [255,255,255]
For some reason discrimage.cols and discrimage.width don't seem to work. I get the following printout: Traceback (most recent call last): File "I2LTEST3.py", line 21, in <module> discrImage = imageToPoints (image) File "I2LTEST3.py", line 14, in imageToPoints for x in range (discrImage.cols): AttributeError: 'tuple' object has no attribute 'cols'
Any ideas? Thanks!
EDIT
So thanks to some guidance I've managed to get the code to work for this much:
def imageToPoints (image):
ret, discrImage = cv.threshold (image, 127, 255, 0)
height, width = discrImage.shape
# Add a line of white values along the top of the image.
# This ensure that the binary outside contour generation works.
# It is removed later before points are written to point cloud.
for x in range (width):
#discrImage[x,0] = 255
return discrImage
Uncommenting discrImage[x,0] = 255 gives the feedback:
Traceback (most recent call last):
File "I2LTEST3.py", line 21, in <module>
discrImage = imageToPoints (image)
File "I2LTEST3.py", line 16, in imageToPoints
discrImage[x,0] = 255
IndexError: index 480 is out of bounds for axis 0 with size 480
Which sounds like a range issue, which I'm going to look into further.
ret, discrImage = cv.threshold(image,127,255,0)
(you had it already correct in the commented part !)
Hi, I've been flitting about all over so haven't actually discovered what ret, actually does. I thought it was a specific way of returning the value even though it seems laid out as an argument assignment? So after getting that to work I commented it out and tried it with "return" at the end of the function. Would you mind explaining it to me while I give this a go? Thanks :)
ret is the threshold value (calculated, if you use THRESH_OTSU) see docs, please
I read that it returns true or false value from getting the frame here. I'll spend some more time wrapping my head around the actual point if it, which the documentation hasn't made clear to me.
^^ that's a different function and a different set of return values.
Point taken! Thanks for your help. I've debugged right through to some IndexError shown in the edit.
both opencv and numpy are row-major, so you have to index it like:
discrImage[y,x]
but you should better avoid writing loops like that, and use a numpy slice, so you can assign the whole 1st row in a single line of code:
No matter, solved it :) Many thanks!!
Yeah, I just noticed that. It's odd because the same layout [x,y] worked in C++. I'll try the code you suggested, looks way neater, thank you!
Next I need to work with contours and I'm just reading through the documentation but I expect some issues too; kind of common when you're self-taught and in the deep end :')