Ask Your Question

Revision history [back]

Get Trackbar Wont Work

Hi guys, i really need help for my project. Im new to python and openCV. I want to create trackbar for detecting the right threshold values for canny edge detection, However, it seems didnt work. Below is the code:

imagesegmentationusingcannyedgedetection

import numpy as np import cv2

def nothing(x): pass

def find_contour(th1,th2):

edges = cv2.Canny(gaus,th1,th2)
contours,hierarchy = cv2.findContours(edges,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
for cnt in contours:
    color = np.random.randint(0,255,(3)).tolist()
    cv2.drawContours(img,[cnt],0,color,2)
    cv2.imshow('output',img)

img=cv2.imread('C:/Users/USER/Desktop/PytonCVImageProcessing/Tutorials/picture/pineapple.jpg') grayscaled = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) gaus=cv2.adaptiveThreshold(grayscaled,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY, 11, 2)

cv2.namedWindow('Result',cv2.WINDOW_NORMAL) cv2.createTrackbar('TH1','Result',0,255,nothing) cv2.createTrackbar('TH2','Result',0,255,nothing)

find_contour(cv2.getTrackbarPos('TH1','Result'),cv2.getTrackbarPos('TH2','Result'))

while True: k = cv2.waitKey(0) & 0xFF
if k == 27: break # ESC key to exit cv2.destroyAllWindows()

click to hide/show revision 2
No.2 Revision

updated 2016-12-27 14:03:29 -0600

berak gravatar image

Get Trackbar Wont Work

Hi guys, i really need help for my project. Im new to python and openCV. I want to create trackbar for detecting the right threshold values for canny edge detection, However, it seems didnt work. Below is the code:

imagesegmentationusingcannyedgedetection

import numpy as np
import cv2

cv2

def nothing(x): pass

pass def find_contour(th1,th2):

def find_contour(th1,th2):

 edges = cv2.Canny(gaus,th1,th2)
 contours,hierarchy = cv2.findContours(edges,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
 for cnt in contours:
  color = np.random.randint(0,255,(3)).tolist()
 cv2.drawContours(img,[cnt],0,color,2)
 cv2.imshow('output',img)

img=cv2.imread('C:/Users/USER/Desktop/PytonCVImageProcessing/Tutorials/picture/pineapple.jpg') grayscaled = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) gaus=cv2.adaptiveThreshold(grayscaled,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY, 11, 2)

cv2.namedWindow('Result',cv2.WINDOW_NORMAL) cv2.createTrackbar('TH1','Result',0,255,nothing) cv2.createTrackbar('TH2','Result',0,255,nothing)

cv2.createTrackbar('TH2','Result',0,255,nothing) find_contour(cv2.getTrackbarPos('TH1','Result'),cv2.getTrackbarPos('TH2','Result'))

find_contour(cv2.getTrackbarPos('TH1','Result'),cv2.getTrackbarPos('TH2','Result'))

while True: k = cv2.waitKey(0) & 0xFF
if k == 27: break # ESC key to exit cv2.destroyAllWindows()

cv2.destroyAllWindows()