def sobel(): cv.Smooth(src_image, dst_image, cv.CV_GAUSSIAN, 3, 3) src_gray = cv.CreateImage((src_image.width, src_image.height), 8, 1) dst_gray1 = cv.CreateImage((src_image.width, src_image.height), 8, 1) dst_gray = cv.CreateImage((src_image.width, src_image.height), 8, 1) cv.CvtColor(src_image, src_gray, cv.CV_BGR2GRAY) cv.Sobel(src_gray, dst_gray1, 0, 1, 3) cv.ConvertScaleAbs(dst_gray1, dst_gray1, 1, 0) cv.Sobel(src_gray, dst_gray, 1, 0, 3) cv.ConvertScaleAbs(dst_gray, dst_gray, 1, 0) cv.AddWeighted(dst_gray, 0.5, dst_gray1, 0.5, 0, dst_gray) cv.NamedWindow("Destination Image") cv.ShowImage("Destination Image", dst_gray) cv.WaitKey(0)
def get_gradient_img(self, input_img): # Generate grad_x and grad_y to use Sobel grad_x=cv.CreateMat(input_img.height, input_img.width, input_img.type) grad_y=cv.CreateMat(input_img.height, input_img.width, input_img.type) abs_grad_x=cv.CreateMat(input_img.height, input_img.width, input_img.type) abs_grad_y=cv.CreateMat(input_img.height, input_img.width, input_img.type) grad =cv.CreateMat(input_img.height, input_img.width, input_img.type) #Gradient X #Scharr( src_gray, grad_x, ddepth, 1, 0, scale, delta, BORDER_DEFAULT );#for small kernels use this instead of sobel cv.Sobel( input_img, grad_x, 1, 0) cv.Abs(grad_x, abs_grad_x ) #Gradient Y #Scharr( src_gray, grad_y, ddepth, 0, 1, scale, delta, BORDER_DEFAULT ); cv.Sobel( input_img, grad_y, 0, 1) cv.Abs( grad_y, abs_grad_y ) #/// Total Gradient (approximate) cv.AddWeighted( abs_grad_x, 0.5, abs_grad_y, 0.5, 0, grad ) return grad
def contour_iterator(contour): while contour: yield contour contour = contour.h_next() cv.Zero(markers) comp_count = 0 for c in contour_iterator(contours): cv.DrawContours(markers, c, cv.ScalarAll(comp_count + 1), cv.ScalarAll(comp_count + 1), -1, -1, 8) comp_count += 1 cv.Watershed(img0, markers) cv.Set(wshed, cv.ScalarAll(255)) # paint the watershed image color_tab = [ (cv.RandInt(rng) % 180 + 50, cv.RandInt(rng) % 180 + 50, cv.RandInt(rng) % 180 + 50) for i in range(comp_count) ] for j in range(markers.height): for i in range(markers.width): idx = markers[j, i] if idx != -1: wshed[j, i] = color_tab[int(idx - 1)] cv.AddWeighted(wshed, 0.5, img_gray, 0.5, 0, wshed) cv.ShowImage("watershed transform", wshed)