Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

OpenCV Python on RPi, can't seem to iterate through image width

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!

OpenCV Python on RPi, can't seem to iterate through image width

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.

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)

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:

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]
[255,255,255]

For some reason discrimage.cols and discrimage.width don't seem to work. I get the following printout: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 14, 16, in imageToPoints
    discrImage[x,1] = 255
IndexError: index 480 is out of bounds for x in range (discrImage.cols):
AttributeError: 'tuple' object has no attribute 'cols'
axis 0 with size 480

Any ideas? Thanks!Which sounds like a range issue, which I'm going to look into further.

OpenCV Python on RPi, can't seem to iterate through image width

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,1] 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.