0

i am trying to write a code using opencv python that automatically get canny threshold values instead of doing them manually every time.

img= cv2.imread('micro.png',0)
output = np.zeros(img.shape, img.dtype)
# Otsu's thresholding
ret2,highthresh = cv2.threshold(img,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)
lowthresh=0.1*highthres
edges = cv2.Canny(img,output,lowthresh,highthresh)
cv2.imshow('canny',edges)

i am getting this error "File "test2.py", line 14, in edges = cv2.Canny(img,output,lowthresh,highthresh) TypeError: only length-1 arrays can be converted to Python scalars"

can anyone help me to sort out this error.thankx in advance

user3778289
  • 323
  • 4
  • 18
  • I think it has problem with png file, I also get the same error. try converting the image to JPG – Akshay Jan 23 '17 at 23:31

2 Answers2

2

It seems like cv2.threshold returns the detected edges, and Canny applies them to the image. The code below worked for me and gave me some nice detected edges in my image.

import cv2

cv2.namedWindow('canny demo')

img= cv2.imread('micro.png',0)
ret2,detected_edges = cv2.threshold(img,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)
edges = cv2.Canny(detected_edges,0.1,1.0)
dst = cv2.bitwise_and(img,img,mask = edges)
cv2.imshow('canny',dst)

if cv2.waitKey(0) == 27:
    cv2.destroyAllWindows()
Andrew Johnson
  • 3,078
  • 1
  • 18
  • 24
  • this is not giving me the require output.and i have to work on more then one pictures thats why i can't set threshold values everytime for that i got the concept from here. http://stackoverflow.com/questions/4292249/automatic-calculation-of-low-and-high-thresholds-for-the-canny-operation-in-open. but it is not working for me – user3778289 Jul 22 '14 at 20:35
1

You are running:

cv2.Canny(img,output,lowthresh,highthresh)

It is looking for

cv2.Canny(img,lowthresh,highthresh,output)

I think the ordering changed in some version, because I have seen references to both.

Emile Bergeron
  • 17,074
  • 5
  • 83
  • 129
Shane
  • 11
  • 1