Python cv2.THRESH_BINARY() Examples

The following are code examples for showing how to use cv2.THRESH_BINARY(). They are extracted from open source Python projects. You can vote up the examples you like or vote down the exmaples you don't like. You can also save this page to your account.

Example 1
Project: PaperHelper   Author: EdgarNg1024   File: squares.py    (license) View Source Project 17 votes vote down vote up
def find_squares(img):
    img = cv2.GaussianBlur(img, (5, 5), 0)
    squares = []
    for gray in cv2.split(img):
        for thrs in xrange(0, 255, 26):
            if thrs == 0:
                bin = cv2.Canny(gray, 0, 50, apertureSize=5)
                bin = cv2.dilate(bin, None)
            else:
                retval, bin = cv2.threshold(gray, thrs, 255, cv2.THRESH_BINARY)
            bin, contours, hierarchy = cv2.findContours(bin, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
            for cnt in contours:
                cnt_len = cv2.arcLength(cnt, True)
                cnt = cv2.approxPolyDP(cnt, 0.02 * cnt_len, True)
                if len(cnt) == 4 and cv2.contourArea(cnt) > 1000 and cv2.isContourConvex(cnt):
                    cnt = cnt.reshape(-1, 2)
                    max_cos = np.max([angle_cos(cnt[i], cnt[(i + 1) % 4], cnt[(i + 2) % 4]) for i in xrange(4)])
                    if max_cos < 0.1:
                        squares.append(cnt)
    return squares 
Example 2
Project: UAV-and-TrueOrtho   Author: LeonChen66   File: DSM_GEN_BD.py    (license) View Source Project 13 votes vote down vote up
def img_fill(im_in, n):  # n = binary image threshold
    th, im_th = cv2.threshold(im_in, n, 255, cv2.THRESH_BINARY);

    # Copy the thresholded image.
    im_floodfill = im_th.copy()

    # Mask used to flood filling.
    # Notice the size needs to be 2 pixels than the image.
    h, w = im_th.shape[:2]
    mask = np.zeros((h + 2, w + 2), np.uint8)

    # Floodfill from point (0, 0)
    cv2.floodFill(im_floodfill, mask, (0, 0), 255);

    # Invert floodfilled image
    im_floodfill_inv = cv2.bitwise_not(im_floodfill)

    # Combine the two images to get the foreground.
    fill_image = im_th | im_floodfill_inv

    return fill_image 
Example 3
Project: beryl   Author: DanielJDufour   File: __init__.py    (license) View Source Project 9 votes vote down vote up
def find_squares(img):
    img = cv2.GaussianBlur(img, (5, 5), 0)
    squares = []
    for gray in cv2.split(img):
        for thrs in xrange(0, 255, 26):
            if thrs == 0:
                bin = cv2.Canny(gray, 0, 50, apertureSize=5)
                bin = cv2.dilate(bin, None)
            else:
                _retval, bin = cv2.threshold(gray, thrs, 255, cv2.THRESH_BINARY)
            contours, _hierarchy = find_contours(bin, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
            for cnt in contours:
                x, y, w, h = cv2.boundingRect(cnt)
                cnt_len = cv2.arcLength(cnt, True)
                cnt = cv2.approxPolyDP(cnt, 0.02*cnt_len, True)
                area = cv2.contourArea(cnt)
                if len(cnt) == 4 and 20 < area < 1000 and cv2.isContourConvex(cnt):
                    cnt = cnt.reshape(-1, 2)
                    max_cos = np.max([angle_cos( cnt[i], cnt[(i+1) % 4], cnt[(i+2) % 4] ) for i in xrange(4)])
                    if max_cos < 0.1:
                        if (1 - (float(w) / float(h)) <= 0.07 and 1 - (float(h) / float(w)) <= 0.07):
                            squares.append(cnt)
    return squares 
Example 4
Project: WebAct   Author: CreatCodeBuild   File: sd.py    (license) View Source Project 8 votes vote down vote up
def threshold(im_gray, method):
    '''
    ??????????thresholding???????????
    ??????thresholding????????OpenCV??
    '''
    if method == 'fixed':
        threshed_im = cv2.threshold(im_gray, 128, 255, cv2.THRESH_BINARY)

    elif method == 'mean':
        threshed_im = cv2.adaptiveThreshold(im_gray, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 15, -22)

    elif method == 'gaussian':
        threshed_im = cv2.adaptiveThreshold(im_gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 5, 7)

    else:
        return None

    return threshed_im 
Example 5
Project: UAV-and-TrueOrtho   Author: LeonChen66   File: BD_edge.py    (license) View Source Project 8 votes vote down vote up
def img_fill(im_in,n):   # n = binary image threshold
    th, im_th = cv2.threshold(im_in, n, 255, cv2.THRESH_BINARY);
     
    # Copy the thresholded image.
    im_floodfill = im_th.copy()
     
    # Mask used to flood filling.
    # Notice the size needs to be 2 pixels than the image.
    h, w = im_th.shape[:2]
    mask = np.zeros((h+2, w+2), np.uint8)
     
    # Floodfill from point (0, 0)
    cv2.floodFill(im_floodfill, mask, (0,0), 255);
     
    # Invert floodfilled image
    im_floodfill_inv = cv2.bitwise_not(im_floodfill)
     
    # Combine the two images to get the foreground.
    fill_image = im_th | im_floodfill_inv
    
    return fill_image 
Example 6
Project: pyhiro   Author: wanweiwei07   File: recognition.py    (license) View Source Project 8 votes vote down vote up
def checkAvailability(sift, tkp, tdes, matchimg):
    """

    :param sift:
    :param tkp:
    :param tdes:sift feature object, template keypoints, and template descriptor
    :param matchimg:
    :return:
    """

    qimg = cv2.imread(matchimg)
    qimggray = cv2.cvtColor(qimg,cv2.COLOR_BGR2GRAY)
    # kernel = np.ones((5,5), np.uint8)
    # qimggray = cv2.erode(qimggray, kernel, iterations=1)
    # ret,threshimg = cv2.threshold(qimggray,100,255,cv2.THRESH_BINARY)
    qkp,qdes = sift.detectAndCompute(qimggray, None)
    # plt.imshow(threshimg, 'gray'), plt.show()

    FLANN_INDEX_KDITREE=0
    index_params=dict(algorithm=FLANN_INDEX_KDITREE,tree=5)
    # FLANN_INDEX_LSH = 6
    # index_params = dict(algorithm=FLANN_INDEX_LSH,
    #                     table_number=12,  # 12
    #                     key_size=20,  # 20
    #                     multi_probe_level=2)  # 2
    search_params = dict(checks = 50)
    flann=cv2.FlannBasedMatcher(index_params,search_params)
    matches=flann.knnMatch(tdes,qdes,k=2)
    goodMatch=[]
    for m_n in matches:
        if len(m_n) != 2:
            continue
        m, n = m_n
        if(m.distance<0.75*n.distance):
            goodMatch.append(m)
    MIN_MATCH_COUNT = 30
    if (len(goodMatch) >= MIN_MATCH_COUNT):
        tp = []
        qp = []

        for m in goodMatch:
            tp.append(tkp[m.queryIdx].pt)
            qp.append(qkp[m.trainIdx].pt)

        tp, qp = np.float32((tp, qp))
        H, status = cv2.findHomography(tp, qp, cv2.RANSAC, 3.0)

        h = timg.shape[0]
        w = timg.shape[1]
        trainBorder = np.float32([[[0, 0], [0, h - 1], [w - 1, h - 1], [w - 1, 0]]])
        queryBorder = cv2.perspectiveTransform(trainBorder, H)
        cv2.polylines(qimg, [np.int32(queryBorder)], True, (0, 255, 0), 5)
        cv2.imshow('result', qimg)
        plt.imshow(qimg, 'gray'), plt.show()
        return True
    else:
        print "Not Enough match found- %d/%d" % (len(goodMatch), MIN_MATCH_COUNT)
        return False
    # cv2.imshow('result', qimg)
    # if cv2.waitKey(10) == ord('q'):
    #     cv2.destroyAllWindows() 
Example 7
Project: qtim_ROP   Author: QTIM-Lab   File: methods.py    (license) View Source Project 8 votes vote down vote up
def binary_morph(img, thresh=50, min_size=None, mask_only=True):

    if min_size is None:  # default to 10% of largest image dimension
        min_size = float(max(img.shape)) * .1

    if len(img.shape) == 3:  # flatten if RGB image
        img = np.mean(img, 2).astype(np.uint8)

    # Apply binary threshold and erode
    ret, thresh_im = cv2.threshold(img, thresh, 255, cv2.THRESH_BINARY)

    # Connected component labelling
    n, labels, stats, centroids = cv2.connectedComponentsWithStats(thresh_im)
    mask = np.zeros_like(labels)

    # Loop through areas in order of size
    areas = [s[4] for s in stats]
    sorted_idx = np.argsort(areas)

    for lidx, cc in zip(sorted_idx, [areas[s] for s in sorted_idx][:-1]):

        if cc > min_size:
            mask[labels == lidx] = 1

    if mask_only:
        return mask * 255
    return np.dstack([img * mask] * 3).astype(np.uint8) 
Example 8
Project: opentrack-prototyping   Author: DaMichel   File: pointextractor.py    (license) View Source Project 8 votes vote down vote up
def threshold_and_label_blobs(intensity, blob_list, threshold):
  # we cheat a little here because we take the list of
  # known blob locations to fill the thresholded areas
  # surrounding the blob centers. This way false blob detections
  # are avoided.
  assert(intensity.dtype == np.uint8)
  W, H = intensity.shape
  _, blobs_mask = cv2.threshold(intensity, threshold, 1, cv2.THRESH_BINARY)
  labels = 255 - blobs_mask
  for num, pk in enumerate(blob_list):
    iy, ix = int(pk.y), int(pk.x)
    if labels[ix, iy] < 254: # was the area under the blob location already filled by another blob?
        return None # overlapping non-separable blobs
    if labels[ix, iy] == 255:
        return None # blob center is not in thresholded area
    cv2.floodFill(labels.reshape((W, H, 1)), None, (iy, ix), (num,), (0,), (0,))
  labels[labels == 254] = 255 # everything not a proper blob is assigned label 255
  return labels 
Example 9
Project: Cross-a-Crater   Author: rishab-sharma   File: imgLib.py    (license) View Source Project 8 votes vote down vote up
def detectCellVal(img_rgb,grid_map):
        zero = cv2.imread('digits/0.jpg')
        one = cv2.imread('digits/1.jpg')
        for i in range(14):
                for j in range(14):
                    px = img_rgb[((50*i)+5):((50*i)+45),((50*j)+5):((50*j)+45)]
                    d1 = cv2.subtract(px,zero[5:45,5:45])
                    gray = cv2.cvtColor(d1,cv2.COLOR_BGR2GRAY)
                    ret, mask = cv2.threshold(gray, 220,255,cv2.THRESH_BINARY)
                    r = not np.any(mask)
                    if r == True:
                        grid_map[i][j]=1
                    d1 = cv2.subtract(px,one[5:45,5:45])
                    gray = cv2.cvtColor(d1,cv2.COLOR_BGR2GRAY)
                    ret, mask = cv2.threshold(gray, 220,255,cv2.THRESH_BINARY)
                    r = not np.any(mask)
                    if r == True:
                        grid_map[i][j]=0
        return grid_map
############################################################################################
# solveGrid finds the shortest path,
# between valid grid cell in the start row 
# and valid grid cell in the destination row 
# solveGrid(grid_map)
# Return the route_path and route_length 
Example 10
Project: piwall-cvtools   Author: infinnovation   File: piwall.py    (license) View Source Project 7 votes vote down vote up
def find_squares(img, cos_limit = 0.1):
    print('search for squares with threshold %f' % cos_limit)
    img = cv2.GaussianBlur(img, (5, 5), 0)
    squares = []
    for gray in cv2.split(img):
        for thrs in xrange(0, 255, 26):
            if thrs == 0:
                bin = cv2.Canny(gray, 0, 50, apertureSize=5)
                bin = cv2.dilate(bin, None)
            else:
                retval, bin = cv2.threshold(gray, thrs, 255, cv2.THRESH_BINARY)
            bin, contours, hierarchy = cv2.findContours(bin, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
            for cnt in contours:
                cnt_len = cv2.arcLength(cnt, True)
                cnt = cv2.approxPolyDP(cnt, 0.02*cnt_len, True)
                if len(cnt) == 4 and cv2.contourArea(cnt) > 1000 and cv2.isContourConvex(cnt):
                    cnt = cnt.reshape(-1, 2)
                    max_cos = np.max([angle_cos( cnt[i], cnt[(i+1) % 4], cnt[(i+2) % 4] ) for i in xrange(4)])
                    if max_cos < cos_limit :
                        squares.append(cnt)
                    else:
                        #print('dropped a square with max_cos %f' % max_cos)
                        pass
    return squares

###
### Version V2.  Collect meta-data along the way,  with commentary added.
### 
Example 11
Project: bib-tagger   Author: KateRita   File: find_bibs.py    (license) View Source Project 7 votes vote down vote up
def find_bibs(image):
  gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY);
  binary = cv2.GaussianBlur(gray,(5,5),0)
  ret,binary = cv2.threshold(binary, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU);
  #binary = cv2.adaptiveThreshold(binary, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 11, 2)
  #ret,binary = cv2.threshold(binary, 190, 255, cv2.THRESH_BINARY);

  #lapl = cv2.Laplacian(image,cv2.CV_64F)
  #gray = cv2.cvtColor(lapl, cv2.COLOR_BGR2GRAY);
  #blurred = cv2.GaussianBlur(lapl,(5,5),0)
  #ret,binary = cv2.threshold(blurred, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU);
  #cv2.imwrite("lapl.jpg", lapl)

  edges = cv2.Canny(image,175,200)
  cv2.imwrite("edges.jpg", edges)
  binary = edges

  cv2.imwrite("binary.jpg", binary)
  contours,hierarchy = find_contours(binary)

  return get_rectangles(contours) 
Example 12
Project: AutomatorX   Author: xiaoyaojjian   File: imutils.py    (license) View Source Project 7 votes vote down vote up
def diff_rect(img1, img2, pos=None):
    """find counters include pos in differences between img1 & img2 (cv2 images)"""
    diff = cv2.absdiff(img1, img2)
    diff = cv2.GaussianBlur(diff, (3, 3), 0)
    edges = cv2.Canny(diff, 100, 200)
    _, thresh = cv2.threshold(edges, 0, 255, cv2.THRESH_BINARY)
    contours, _ = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
    if not contours:
        return None
    contours.sort(key=lambda c: len(c))
    # no pos provide, just return the largest different area rect
    if pos is None:
        cnt = contours[-1]
        x0, y0, w, h = cv2.boundingRect(cnt)
        x1, y1 = x0+w, y0+h
        return (x0, y0, x1, y1)
    # else the rect should contain the pos
    x, y = pos
    for i in range(len(contours)):
        cnt = contours[-1-i]
        x0, y0, w, h = cv2.boundingRect(cnt)
        x1, y1 = x0+w, y0+h
        if x0 <= x <= x1 and y0 <= y <= y1:
            return (x0, y0, x1, y1) 
Example 13
Project: retinal-exudates-detection   Author: getsanjeev   File: training.py    (license) View Source Project 7 votes vote down vote up
def calculate_entropy(image):
	entropy = image.copy()
	sum = 0
	i = 0
	j = 0
	while i < entropy.shape[0]:
		j = 0
		while j < entropy.shape[1]:
			sub_image = entropy[i:i+10,j:j+10]
			histogram = cv2.calcHist([sub_image],[0],None,[256],[0,256])
			sum = 0
			for k in range(256):
				if histogram[k] != 0:					
					sum = sum + (histogram[k] * math.log(histogram[k]))
				k = k + 1
			entropy[i:i+10,j:j+10] = sum
			j = j+10
		i = i+10
	ret2,th2 = cv2.threshold(entropy,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)
	newfin = cv2.erode(th2, cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(3,3)), iterations=1)
	return newfin 
Example 14
Project: WebAct   Author: CreatCodeBuild   File: sd.py    (license) View Source Project 7 votes vote down vote up
def threshold(im_gray, method):
    '''
    ??????????thresholding???????????
    ??????thresholding????????OpenCV??
    '''
    if method == 'fixed':
        threshed_im = cv2.threshold(im_gray, 128, 255, cv2.THRESH_BINARY)

    elif method == 'mean':
        threshed_im = cv2.adaptiveThreshold(im_gray, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 15, -22)

    elif method == 'gaussian':
        threshed_im = cv2.adaptiveThreshold(im_gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 5, 7)

    else:
        return None

    return threshed_im 
Example 15
Project: Artificial-Potential-Field   Author: vampcoder   File: Artificial-potential-controller-2.py    (license) View Source Project 7 votes vote down vote up
def classify(img):
    cimg = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    img2 = cv2.medianBlur(cimg, 13)

    ret, thresh1 = cv2.threshold(cimg, 100, 120, cv2.THRESH_BINARY)
    t2 = copy.copy(thresh1)

    x, y = thresh1.shape
    arr = np.zeros((x, y, 3), np.uint8)
    final_contours = []
    image, contours, hierarchy = cv2.findContours(t2, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
    #cv2.imshow('image', image)
    #k = cv2.waitKey(0)
    for i in range(len(contours)):
        cnt = contours[i]
        if cv2.contourArea(cnt) > 3000 and cv2.contourArea(cnt) < 25000:
            cv2.drawContours(img, [cnt], -1, [0, 255, 255])
            cv2.fillConvexPoly(arr, cnt, [255, 255, 255])
            final_contours.append(cnt)
    #cv2.imshow('arr', arr)
    #k = cv2.waitKey(0)
    return arr 
Example 16
Project: SummerProject_MacularDegenerationDetection   Author: WDongYuan   File: FeatureExtraction.py    (license) View Source Project 6 votes vote down vote up
def CropLowerBoundary(img):
	# img_gray = ToGrayImage(path)
	_,img_bi = cv2.threshold(img,60,255,cv2.THRESH_BINARY)
	threshold_rate = 0.95
	threshold_row = -1
	row,col = img_bi.shape
	for tmp_r in range(row-1,-1,-1):
		tmp_sum = sum(img_bi[tmp_r])
		rate = float(tmp_sum)/255/col
		# print(rate)
		if rate>threshold_rate:
			threshold_row = tmp_r
			break
	img = img[0:threshold_row,:]
	# plt.imshow(img,"gray")
	# plt.show()
	return img 
Example 17
Project: SummerProject_MacularDegenerationDetection   Author: WDongYuan   File: BoundaryExtraction.py    (license) View Source Project 6 votes vote down vote up
def FindLowerBoundary(path,mode):
	img_gray = ToGrayImage(path)
	_,img_bi = cv2.threshold(img_gray,10,255,cv2.THRESH_BINARY)
	threshold_rate = 0.8
	threshold_row = -1
	row,col = img_bi.shape
	for tmp_r in range(row-1,-1,-1):
		tmp_sum = sum(img_bi[tmp_r])
		rate = float(tmp_sum)/255/col
		if rate>threshold_rate:
			threshold_row = tmp_r
			break
	return threshold_row



# GrayScale Image Convertor
# https://extr3metech.wordpress.com 
Example 18
Project: SummerProject_MacularDegenerationDetection   Author: WDongYuan   File: BoundaryExtraction.py    (license) View Source Project 6 votes vote down vote up
def FindLowerBoundary(path,mode):
	img_gray = ToGrayImage(path)
	_,img_bi = cv2.threshold(img_gray,10,255,cv2.THRESH_BINARY)
	threshold_rate = 0.8
	threshold_row = -1
	row,col = img_bi.shape
	for tmp_r in range(row-1,-1,-1):
		tmp_sum = sum(img_bi[tmp_r])
		rate = float(tmp_sum)/255/col
		if rate>threshold_rate:
			threshold_row = tmp_r
			break
	return threshold_row



# GrayScale Image Convertor
# https://extr3metech.wordpress.com 
Example 19
Project: reconstruction   Author: microelly2   File: CV2.py    (license) View Source Project 6 votes vote down vote up
def execute_Threshold(proxy,obj):

	try: img=obj.sourceObject.Proxy.img.copy()
	except: img=cv2.imread(__dir__+'/icons/freek.png')

	# img = cv2.imread('dave.jpg',0) ??
	img = cv2.medianBlur(img,5)
	img = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)


	if obj.globalThresholding:
		ret,th1 = cv2.threshold(img,obj.param1,obj.param2,cv2.THRESH_BINARY)
		obj.Proxy.img = cv2.cvtColor(th1, cv2.COLOR_GRAY2RGB)

	if obj.adaptiveMeanTresholding:
		th2 = cv2.adaptiveThreshold(img,255,cv2.ADAPTIVE_THRESH_MEAN_C,\
				cv2.THRESH_BINARY,11,2)
		obj.Proxy.img = cv2.cvtColor(th2, cv2.COLOR_GRAY2RGB)

	if obj.adaptiveGaussianThresholding:
		th3 = cv2.adaptiveThreshold(img,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,\
			cv2.THRESH_BINARY,17,2)
		obj.Proxy.img = cv2.cvtColor(th3, cv2.COLOR_GRAY2RGB) 
Example 20
Project: ConditionalGAN   Author: seungjooli   File: model.py    (license) View Source Project 6 votes vote down vote up
def detect_edges(images):
        def blur(image):
            return cv2.GaussianBlur(image, (5, 5), 0)

        def canny_otsu(image):
            scale_factor = 255
            scaled_image = np.uint8(image * scale_factor)

            otsu_threshold = cv2.threshold(
                cv2.cvtColor(scaled_image, cv2.COLOR_RGB2GRAY), 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)[0]
            lower_threshold = max(0, int(otsu_threshold * 0.5))
            upper_threshold = min(255, int(otsu_threshold))
            edges = cv2.Canny(scaled_image, lower_threshold, upper_threshold)
            edges = cv2.cvtColor(edges, cv2.COLOR_GRAY2RGB)

            return np.float32(edges) * (1 / scale_factor)

        blurred = [blur(image) for image in images]
        canny_applied = [canny_otsu(image) for image in blurred]

        return canny_applied 
Example 21
Project: piwall-cvtools   Author: infinnovation   File: piwall.py    (license) View Source Project 6 votes vote down vote up
def cannyThresholding(self, contour_retrieval_mode = cv2.RETR_LIST):
        '''
        contour_retrieval_mode is passed through as second argument to cv2.findContours
        '''
    
        # Attempt to match edges found in blue, green or red channels : collect all
        channel = 0
        for gray in cv2.split(self.img):
            channel += 1
            print('channel %d ' % channel)
            title = self.tgen.next('channel-%d' % channel)
            if self.show: ImageViewer(gray).show(window = title, destroy = self.destroy, info = self.info, thumbnailfn = title)
            found = {}
            for thrs in xrange(0, 255, 26):
                print('Using threshold %d' % thrs)
                if thrs == 0:
                    print('First step')
                    bin = cv2.Canny(gray, 0, 50, apertureSize=5)
                    title = self.tgen.next('canny-%d' % channel)
                    if self.show: ImageViewer(bin).show(window = title, destroy = self.destroy, info = self.info, thumbnailfn = title)
                    bin = cv2.dilate(bin, None)
                    title = self.tgen.next('canny-dilate-%d' % channel)
                    if self.show: ImageViewer(bin).show(window = title, destroy = self.destroy, info = self.info, thumbnailfn = title)
                else:
                    retval, bin = cv2.threshold(gray, thrs, 255, cv2.THRESH_BINARY)
                    title = self.tgen.next('channel-%d-threshold-%d' % (channel, thrs))
                    if self.show: ImageViewer(bin).show(window='Next threshold (n to continue)', destroy = self.destroy, info = self.info, thumbnailfn = title)
                bin, contours, hierarchy = cv2.findContours(bin, contour_retrieval_mode, cv2.CHAIN_APPROX_SIMPLE)
                title = self.tgen.next('channel-%d-threshold-%d-contours' % (channel, thrs))
                if self.show: ImageViewer(bin).show(window = title, destroy = self.destroy, info = self.info, thumbnailfn = title)
                if contour_retrieval_mode == cv2.RETR_LIST or contour_retrieval_mode == cv2.RETR_EXTERNAL:
                    filteredContours = contours
                else:
                    filteredContours = []
                    h = hierarchy[0]
                    for component in zip(contours, h):
                        currentContour = component[0]
                        currentHierarchy = component[1]
                        if currentHierarchy[3] < 0:
                            # Found the outermost parent component
                            filteredContours.append(currentContour)
                    print('Contours filtered.   Input %d  Output %d' % (len(contours), len(filteredContours)))
                    time.sleep(5)
                for cnt in filteredContours:
                    cnt_len = cv2.arcLength(cnt, True)
                    cnt = cv2.approxPolyDP(cnt, 0.02*cnt_len, True)
                    cnt_len = len(cnt)
                    cnt_area = cv2.contourArea(cnt)
                    cnt_isConvex = cv2.isContourConvex(cnt)
                    if cnt_len == 4 and (cnt_area > self.area_min and cnt_area < self.area_max)  and cnt_isConvex:
                        cnt = cnt.reshape(-1, 2)
                        max_cos = np.max([angle_cos( cnt[i], cnt[(i+1) % 4], cnt[(i+2) % 4] ) for i in xrange(4)])
                        if max_cos < self.cos_limit :
                            sq = Square(cnt, cnt_area, cnt_isConvex, max_cos)
                            self.squares.append(sq)
                        else:
                            #print('dropped a square with max_cos %f' % max_cos)
                            pass
                found[thrs] = len(self.squares)
                print('Found %d quadrilaterals with threshold %d' % (len(self.squares), thrs)) 
Example 22
Project: bib-tagger   Author: KateRita   File: find_bibs.py    (license) View Source Project 6 votes vote down vote up
def find_bib(image):
  width, height, depth = image.shape

  gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY);
  #gray = cv2.equalizeHist(gray)
  blurred = cv2.GaussianBlur(gray,(5,5),0)

  debug_output("find_bib_blurred", blurred)
  #binary = cv2.adaptiveThreshold(blurred, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, blockSize=25, C=0);
  ret,binary = cv2.threshold(blurred, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU);
  #ret,binary = cv2.threshold(blurred, 170, 255, cv2.THRESH_BINARY);
  debug_output("find_bib_binary", binary)
  threshold_contours,hierarchy = find_contours(binary)

  debug_output("find_bib_threshold", binary)

  edges = cv2.Canny(gray,175,200, 3)
  edge_contours,hierarchy = find_contours(edges)

  debug_output("find_bib_edges", edges)

  contours = threshold_contours + edge_contours
  debug_output_contours("find_bib_threshold_contours", image, contours)

  rectangles = get_rectangles(contours)

  debug_output_contours("find_bib_rectangles", image, rectangles)

  potential_bibs = [rect for rect in rectangles if is_potential_bib(rect, width*height)]

  debug_output_contours("find_bib_potential_bibs", image, potential_bibs)

  ideal_aspect_ratio = 1.0
  potential_bibs = sorted(potential_bibs, key = lambda bib: abs(aspect_ratio(bib) - ideal_aspect_ratio))

  return potential_bibs[0] if len(potential_bibs) > 0 else np.array([[(0,0)],[(0,0)],[(0,0)],[(0,0)]])

#
# Checks that the size and aspect ratio of the contour is appropriate for a bib.
# 
Example 23
Project: ATX   Author: NetEaseGame   File: imutils.py    (license) View Source Project 6 votes vote down vote up
def diff_rect(img1, img2, pos=None):
    """find counters include pos in differences between img1 & img2 (cv2 images)"""
    diff = cv2.absdiff(img1, img2)
    diff = cv2.GaussianBlur(diff, (3, 3), 0)
    edges = cv2.Canny(diff, 100, 200)
    _, thresh = cv2.threshold(edges, 0, 255, cv2.THRESH_BINARY)
    contours, _ = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
    if not contours:
        return None
    contours.sort(key=lambda c: len(c))
    # no pos provide, just return the largest different area rect
    if pos is None:
        cnt = contours[-1]
        x0, y0, w, h = cv2.boundingRect(cnt)
        x1, y1 = x0+w, y0+h
        return (x0, y0, x1, y1)
    # else the rect should contain the pos
    x, y = pos
    for i in range(len(contours)):
        cnt = contours[-1-i]
        x0, y0, w, h = cv2.boundingRect(cnt)
        x1, y1 = x0+w, y0+h
        if x0 <= x <= x1 and y0 <= y <= y1:
            return (x0, y0, x1, y1) 
Example 24
Project: cervix-roi-segmentation-by-unet   Author: scottykwok   File: crop.py    (license) View Source Project 6 votes vote down vote up
def cropCircle(img, resize=None):
    if resize:
        if (img.shape[0] > img.shape[1]):
            tile_size = (int(img.shape[1] * resize / img.shape[0]), resize)
        else:
            tile_size = (resize, int(img.shape[0] * resize / img.shape[1]))
        img = cv2.resize(img, dsize=tile_size, interpolation=cv2.INTER_CUBIC)
    else:
        tile_size = img.shape

    gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY);
    _, thresh = cv2.threshold(gray, 10, 255, cv2.THRESH_BINARY)

    _, contours, _ = cv2.findContours(thresh.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)

    main_contour = sorted(contours, key=cv2.contourArea, reverse=True)[0]

    ff = np.zeros((gray.shape[0], gray.shape[1]), 'uint8')
    cv2.drawContours(ff, main_contour, -1, 1, 15)
    ff_mask = np.zeros((gray.shape[0] + 2, gray.shape[1] + 2), 'uint8')
    cv2.floodFill(ff, ff_mask, (int(gray.shape[1] / 2), int(gray.shape[0] / 2)), 1)

    rect = maxRect(ff)
    rectangle = [min(rect[0], rect[2]), max(rect[0], rect[2]), min(rect[1], rect[3]), max(rect[1], rect[3])]
    img_crop = img[rectangle[0]:rectangle[1], rectangle[2]:rectangle[3]]
    cv2.rectangle(ff, (min(rect[1], rect[3]), min(rect[0], rect[2])), (max(rect[1], rect[3]), max(rect[0], rect[2])), 3,
                  2)

    return [img_crop, rectangle, tile_size] 
Example 25
Project: skastic   Author: mypalmike   File: page.py    (license) View Source Project 6 votes vote down vote up
def load(self, filename, analyze_only):
    # Load image, then do various conversions and thresholding.
    self.img_orig = cv2.imread(filename, cv2.IMREAD_COLOR)

    if self.img_orig is None:
      raise CompilerException("File '{}' not found".format(filename))

    self.img_grey = cv2.cvtColor(self.img_orig, cv2.COLOR_BGR2GRAY)
    _, self.img_contour = cv2.threshold(self.img_grey, 250, 255, cv2.THRESH_BINARY_INV)
    _, self.img_text = cv2.threshold(self.img_grey, 150, 255, cv2.THRESH_BINARY)
    self.root_node = None

    self.contours = self.find_contours()

    self.contour_lines, self.contour_nodes = self.categorize_contours()

    self.build_graph()
    self.build_parse_tree()

    self.parse_nodes()

    if not analyze_only:
      self.python_ast = self.root_node.to_python_ast() 
Example 26
Project: AutonomousParking   Author: jovanduy   File: manual_park.py    (license) View Source Project 6 votes vote down vote up
def overlay_img(self):
        """Overlay the transparent, transformed image of the arc onto our CV image"""
        #overlay the arc on the image
        rows, cols, channels = self.transformed.shape
        roi = self.cv_image[0:rows, 0:cols]

        #change arc_image to grayscale
        arc2gray = cv2.cvtColor(self.transformed, cv2.COLOR_BGR2GRAY)
        ret, mask = cv2.threshold(arc2gray, 10, 255, cv2.THRESH_BINARY)
        mask_inv = cv2.bitwise_not(mask)

        #black out area of arc in ROI
        img1_bg = cv2.bitwise_and(roi, roi, mask=mask_inv)
        img2_fg = cv2.bitwise_and(self.transformed, self.transformed, mask=mask)

        #put arc on ROI and modify the main image
        dst = cv2.add(img1_bg, img2_fg)
        self.cv_image[0:rows, 0:cols] = dst 
Example 27
Project: python-image-processing   Author: karaage0703   File: extract_color.py    (license) View Source Project 6 votes vote down vote up
def extract_color( src, h_th_low, h_th_up, s_th, v_th ):
    hsv = cv2.cvtColor(src, cv2.COLOR_BGR2HSV)
    h, s, v = cv2.split(hsv)
    if h_th_low > h_th_up:
        ret, h_dst_1 = cv2.threshold(h, h_th_low, 255, cv2.THRESH_BINARY) 
        ret, h_dst_2 = cv2.threshold(h, h_th_up,  255, cv2.THRESH_BINARY_INV)

        dst = cv2.bitwise_or(h_dst_1, h_dst_2)
    else:
        ret, dst = cv2.threshold(h,   h_th_low, 255, cv2.THRESH_TOZERO) 
        ret, dst = cv2.threshold(dst, h_th_up,  255, cv2.THRESH_TOZERO_INV)
        ret, dst = cv2.threshold(dst, 0, 255, cv2.THRESH_BINARY)

    ret, s_dst = cv2.threshold(s, s_th, 255, cv2.THRESH_BINARY)
    ret, v_dst = cv2.threshold(v, v_th, 255, cv2.THRESH_BINARY)
    dst = cv2.bitwise_and(dst, s_dst)
    dst = cv2.bitwise_and(dst, v_dst)
    return dst 
Example 28
Project: tbotnav   Author: patilnabhi   File: fingers_recog.py    (license) View Source Project 6 votes vote down vote up
def _extract_arm(self, img):
		# find center region of image frame (assume center region is 21 x 21 px)
		center_half = 10 # (=(21-1)/2)	
		center = img[self.height/2 - center_half : self.height/2 + center_half, self.width/2 - center_half : self.width/2 + center_half]

		# determine median depth value
		median_val = np.median(center)

		'''mask the image such that all pixels whose depth values
		lie within a particular range are gray and the rest are black
		'''

		img = np.where(abs(img-median_val) <= self.abs_depth_dev, 128, 0).astype(np.uint8)

		# Apply morphology operation to fill small holes in the image
		kernel = np.ones((5,5), np.uint8)
		img = cv2.morphologyEx(img, cv2.MORPH_CLOSE, kernel)

		# Find connected regions (to hand) to remove background objects
		# Use floodfill with a small image area (7 x 7 px) that is set gray color value
		kernel2 = 3
		img[self.height/2-kernel2:self.height/2+kernel2, self.width/2-kernel2:self.width/2+kernel2] = 128
		
		# a black mask to mask the 'non-connected' components black
		mask = np.zeros((self.height + 2, self.width + 2), np.uint8)
		floodImg = img.copy()

		# Use floodFill function to paint the connected regions white 
		cv2.floodFill(floodImg, mask, (self.width/2, self.height/2), 255, flags=(4 | 255 << 8))
		
		# apply a binary threshold to show only connected hand region
		ret, floodedImg = cv2.threshold(floodImg, 129, 255, cv2.THRESH_BINARY)

		return floodedImg 
Example 29
Project: HandwritingRecognition   Author: eng-tsmith   File: preprocessor_eval.py    (license) View Source Project 6 votes vote down vote up
def thresholding(img_grey):
    """
    This functions creates binary images using thresholding
    :param img_grey: greyscale image
    :return: binary image
    """
    # # Adaptive Gaussian
    # img_binary = cv.adaptiveThreshold(img_grey, 255, cv.ADAPTIVE_THRESH_GAUSSIAN_C, cv.THRESH_BINARY, 11, 2)

    # Otsu's thresholding after Gaussian filtering
    blur = cv.GaussianBlur(img_grey, (5, 5), 0)
    ret3, img_binary = cv.threshold(blur, 0, 255, cv.THRESH_BINARY + cv.THRESH_OTSU)

    # invert black = 255
    ret, thresh1 = cv.threshold(img_binary, 127, 255, cv.THRESH_BINARY_INV)

    return thresh1 
Example 30
Project: HandwritingRecognition   Author: eng-tsmith   File: preprocessor.py    (license) View Source Project 6 votes vote down vote up
def thresholding(img_grey):
    """
    This functions creates binary images using thresholding
    :param img_grey: greyscale image
    :return: binary image
    """
    # # Global
    # ret1, thresh1 = cv.threshold(img_grey, 127, 255, cv.THRESH_BINARY_INV)
    # show_img(thresh1)
    #
    # # Adaptive Mean
    # img_binary = cv.adaptiveThreshold(img_grey, 255, cv.ADAPTIVE_THRESH_MEAN_C, cv.THRESH_BINARY, 11, 2)
    # ret2, thresh2 = cv.threshold(img_binary, 127, 255, cv.THRESH_BINARY_INV)
    # show_img(thresh2)
    #
    # # Adaptive Gaussian
    # img_binary = cv.adaptiveThreshold(img_grey, 255, cv.ADAPTIVE_THRESH_GAUSSIAN_C, cv.THRESH_BINARY, 11, 2)
    # ret3, thresh3 = cv.threshold(img_binary, 127, 255, cv.THRESH_BINARY_INV)
    # show_img(thresh3)

    # Otsu's thresholding after Gaussian filtering
    blur = cv.GaussianBlur(img_grey, (5, 5), 0)
    ret4, img_otsu = cv.threshold(blur, 0, 255, cv.THRESH_BINARY + cv.THRESH_OTSU)
    ret4, thresh4 = cv.threshold(img_otsu, 127, 255, cv.THRESH_BINARY_INV)
    # show_img(thresh4)

    return thresh4 
Example 31
Project: Artificial-Potential-Field   Author: vampcoder   File: Artificial-potential-without-controller.py    (license) View Source Project 6 votes vote down vote up
def classify(img):
    cimg = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    img2 = cv2.medianBlur(cimg, 13)

    ret, thresh1 = cv2.threshold(cimg, 100, 120, cv2.THRESH_BINARY)
    t2 = copy.copy(thresh1)

    x, y = thresh1.shape
    arr = np.zeros((x, y, 3), np.uint8)
    final_contours = []
    image, contours, hierarchy = cv2.findContours(t2, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
    #cv2.imshow('image', image)
    #k = cv2.waitKey(0)
    for i in range(len(contours)):
        cnt = contours[i]
        if cv2.contourArea(cnt) > 35000 and cv2.contourArea(cnt) < 15000:
            cv2.drawContours(img, [cnt], -1, [0, 255, 255])
            cv2.fillConvexPoly(arr, cnt, [255, 255, 255])
            final_contours.append(cnt)
    cv2.imshow('arr', arr)
    k = cv2.waitKey(0)
    return arr 
Example 32
Project: rosreestr2coord   Author: rendrom   File: parser.py    (license) View Source Project 6 votes vote down vote up
def get_image_xy_corner(self):
        """get ?artesian coordinates from raster"""
        import cv2

        if not self.image_path:
            return False
        image_xy_corners = []
        img = cv2.imread(self.image_path, cv2.IMREAD_GRAYSCALE)
        imagem = (255 - img)

        try:
            ret, thresh = cv2.threshold(imagem, 10, 128, cv2.THRESH_BINARY)
            try:
                contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
            except Exception:
                im2, contours, hierarchy = cv2.findContours(thresh, cv2.RETR_CCOMP, cv2.CHAIN_APPROX_SIMPLE)

            hierarchy = hierarchy[0]
            hierarhy_contours = [[] for _ in range(len(hierarchy))]
            for fry in range(len(contours)):
                currentContour = contours[fry]
                currentHierarchy = hierarchy[fry]
                cc = []
                # epsilon = 0.0005 * cv2.arcLength(contours[len(contours) - 1], True)
                approx = cv2.approxPolyDP(currentContour, self.epsilon, True)
                if len(approx) > 2:
                    for c in approx:
                        cc.append([c[0][0], c[0][1]])
                    parent_index = currentHierarchy[3]
                    index = fry if parent_index < 0 else parent_index
                    hierarhy_contours[index].append(cc)

            image_xy_corners = [c for c in hierarhy_contours if len(c) > 0]
            return image_xy_corners
        except Exception as ex:
            self.error(ex)
        return image_xy_corners 
Example 33
Project: opencv-plgs   Author: Image-Py   File: athreshold_plg.py    (license) View Source Project 6 votes vote down vote up
def run(self, ips, snap, img, para = None):
        med = cv2.ADAPTIVE_THRESH_MEAN_C if para['med']=='mean' else cv2.ADAPTIVE_THRESH_GAUSSIAN_C
        mtype = cv2.THRESH_BINARY_INV if para['inv'] else cv2.THRESH_BINARY
        cv2.adaptiveThreshold(snap, para['max'], med, para['inv'], para['size'], para['offset'], dst=img) 
Example 34
Project: dust_repos   Author: taozhijiang   File: dushu.py    (license) View Source Project 6 votes vote down vote up
def img_sobel_binary(im, blur_sz):
    
    # ??????????????
    img_blur = cv2.GaussianBlur(im,blur_sz,0)
    if len(img_blur.shape) == 3:
        blur_gray = cv2.cvtColor(img_blur,cv2.COLOR_BGR2GRAY) 
    else:
        blur_gray = img_blur
    
    # ??Sobel????
    sobelx = cv2.Sobel(blur_gray,cv2.CV_16S,1,0,ksize=3)
    abs_sobelx = np.absolute(sobelx)
    sobel_8u = np.uint8(abs_sobelx)
    img_show_hook("Sobel??", sobel_8u)
    
    # OTSU??????    
    ret, thd = cv2.threshold(sobel_8u, 0, 255, cv2.THRESH_BINARY+cv2.THRESH_OTSU)    
    thd_abs = cv2.convertScaleAbs(thd)
    bgimg = cv2.addWeighted(thd_abs, 1, 0, 0, 0)
    
    img_show_hook("OTSU????", bgimg)
    
    return bgimg 
Example 35
Project: dust_repos   Author: taozhijiang   File: idcard.py    (license) View Source Project 6 votes vote down vote up
def img_sobel_binary(im, blur_sz):
    
    # ??????????????
    img_blur = cv2.GaussianBlur(im,blur_sz,0)
    if len(img_blur.shape) == 3:
        blur_gray = cv2.cvtColor(img_blur,cv2.COLOR_BGR2GRAY) 
    else:
        blur_gray = img_blur
    
    # ??Sobel????
    sobelx = cv2.Sobel(blur_gray,cv2.CV_16S,1,0,ksize=3)
    abs_sobelx = np.absolute(sobelx)
    sobel_8u = np.uint8(abs_sobelx)
    img_show_hook("Sobel??", sobel_8u)
    
    # OTSU??????    
    ret, thd = cv2.threshold(sobel_8u, 0, 255, cv2.THRESH_BINARY+cv2.THRESH_OTSU)    
    thd_abs = cv2.convertScaleAbs(thd)
    bgimg = cv2.addWeighted(thd_abs, 1, 0, 0, 0)
    
    img_show_hook("OTSU????", bgimg)
    
    return bgimg 
Example 36
Project: diagnose-heart   Author: woshialex   File: dsb_utils.py    (license) View Source Project 6 votes vote down vote up
def clean_segmentation2(segments, img_size):
    seg_binary = np.zeros_like(segments)
    for i in xrange(segments.shape[0]):
        _,sb = cv2.threshold(np.copy(segments[i,0])*255, 127, 255, cv2.THRESH_BINARY)
        patches = get_patches(sb)
        if len(patches)==0:
            continue
        sum_pdf_vals = [sum(pdf_dict[x,y] for x,y in p) for p in patches]
        avg_pdf_vals = [sum(pdf_dict[x,y] for x,y in p)/p.shape[0] for p in patches]
        max_sum_pdf = max(sum_pdf_vals)
        for p_idx, p in enumerate(patches):
            if avg_pdf_vals[p_idx] < 0.07 or sum_pdf_vals[p_idx] < max_sum_pdf:
                for x,y in p:
                    seg_binary[i,0,x,y]=0
            else:
                for x,y in p:
                    seg_binary[i,0,x,y]=1
    return seg_binary 
Example 37
Project: diagnose-heart   Author: woshialex   File: dsb_utils.py    (license) View Source Project 6 votes vote down vote up
def segment_ch4(self, segment_fn, segment_transform):
        segs = np.zeros_like(self.ch4_images, dtype=np.float32)
        ims = np.copy(self.ch4_images).reshape(-1, 1, self.ch4_images.shape[1],
                self.ch4_images.shape[2])
        ims = segment_transform(ims)
        for i in xrange(self.ch4_images.shape[0]):
            segs[i:i+1] = segment_fn(ims[i:i+1])
            _,sb = cv2.threshold(np.copy(segs[i])*255, 127, 255, cv2.THRESH_BINARY)
            patches = get_patches(sb)
            sb = np.zeros_like(sb, dtype=np.uint8)
            if len(patches) > 0:
                patch = next(p for p in patches if p.shape[0] == max(p1.shape[0]
                    for p1 in patches))
                for x,y in patch:
                    sb[x,y]=255
                pca = decomposition.PCA(n_components=2)
                pca.fit(patch)
                mean, major = pca.mean_, pca.components_[0]
                middle = sb.shape[0]/2
                sb = cv2.warpAffine(sb, np.float32([[1,0,middle-mean[1]],
                    [0,1,middle-mean[0]]]), sb.shape)
                sb = scipy.misc.imrotate(sb, np.arctan2(*major)*180/np.pi)
            segs[i:i+1]=sb
        self.ch4seg = segs
        self.ch4counts = np.array([np.count_nonzero(s) for s in self.ch4seg]).reshape(1,-1) 
Example 38
Project: diagnose-heart   Author: woshialex   File: dsb_utils.py    (license) View Source Project 6 votes vote down vote up
def clean_segmentation(segments, img_size, thresh):
    mean = segments.mean(axis=(0,1))
    gaussian_params = gaussian_moments_fake(mean, normalize_height=True)
    pdf = gaussian_pdf(*gaussian_params)
    seg_binary = np.zeros_like(segments)
    pdf_dict = np.zeros_like(mean)
    for x in xrange(mean.shape[0]):
        for y in xrange(mean.shape[1]):
            pdf_dict[x,y] = pdf(x,y)
    for i in xrange(segments.shape[0]):
        _,sb = cv2.threshold(np.copy(segments[i,0])*255, 127, 255, cv2.THRESH_BINARY)
        patches = get_patches(sb)
        if len(patches)==0:
            continue
        sum_pdf_vals = [sum(pdf_dict[x,y] for x,y in p) for p in patches]
        avg_pdf_vals = [sum(pdf_dict[x,y] for x,y in p)/p.shape[0] for p in patches]
        max_sum_pdf = max(sum_pdf_vals)
        for p_idx, p in enumerate(patches):
            if avg_pdf_vals[p_idx] < 0.07 or sum_pdf_vals[p_idx] < max_sum_pdf:
                for x,y in p:
                    seg_binary[i,0,x,y]=0
            else:
                for x,y in p:
                    seg_binary[i,0,x,y]=1
    return seg_binary 
Example 39
Project: ATLeS   Author: liffiton   File: tracking.py    (license) View Source Project 6 votes vote down vote up
def _do_filter(self, frame):
        ''' Process a single frame. '''
        # blur to reduce noise
        frame = cv2.GaussianBlur(frame, (5, 5), 0, borderType=cv2.BORDER_CONSTANT)

        # threshold to find contiguous regions of "bright" pixels
        # ignore all "dark" (<1/8 max) pixels
        max = numpy.max(frame)
        min = numpy.min(frame)
        # if the frame is completely dark, then just return it
        if max == min:
            return frame
        threshold = min + (max - min) / 8
        _, frame = cv2.threshold(frame, threshold, 255, cv2.THRESH_BINARY)

        # filter out single pixels and other noise
        frame = cv2.erode(frame, self._element_shrink)

        # restore and join nearby regions (in case one fish has a skinny middle...)
        frame = cv2.dilate(frame, self._element_grow)

        return frame 
Example 40
Project: ETS2Autopilot   Author: BrunoTh   File: speed_detection.py    (license) View Source Project 6 votes vote down vote up
def get_speed(frame):
    """
    :param frame: Captured image
    :return: Speed
    """
    # Disable speed detection
    return 0
    speed = frame[settings.IMAGE_SPEED_Y[0]:settings.IMAGE_SPEED_Y[1], settings.IMAGE_SPEED_X[0]:settings.IMAGE_SPEED_X[1]]
    speed_gray = cv2.cvtColor(speed, cv2.COLOR_BGR2GRAY)

    # Zoom
    rows, cols = speed_gray.shape[:2]
    M = np.float32([[2, 0, 0], [0, 2, 0]])
    speed_zoom = cv2.warpAffine(speed_gray, M, (cols * 2, rows * 2))
    _, speed_threshold = cv2.threshold(speed_zoom, 210, 255, cv2.THRESH_BINARY)

    to_detect = speed_threshold[:, 26:]
    #cv2.imshow('speed', to_detect)
    to_detect = cv2.resize(to_detect, (20, 20))
    to_detect = to_detect.reshape((1, 400))
    to_detect = np.float32(to_detect)
    _, results, _, _ = model.findNearest(to_detect, k=1)

    return int((results[0][0])) 
Example 41
Project: Farmbot_GeneralAP   Author: SpongeYao   File: class_PlantIdentifier.py    (license) View Source Project 6 votes vote down vote up
def extractPlantsArea(self, arg_mode=0,arg_INV= False, b_threshold=80, a_threshold=80):
        zeros = np.zeros(self.image.shape[:2], dtype = "uint8")

        imgLAB = cv2.cvtColor(self.image, self.colorSpace)
        (L, A, B) = cv2.split(imgLAB)
        cv2.imwrite('Debug/imgB.jpg',B)
        cv2.imwrite('Debug/imgA.jpg',A)
        #(T_weeds_b, thresh_weeds_b) = cv2.threshold(B, b_threshold, 255, cv2.THRESH_BINARY)
        #(T_weeds_a, thresh_weeds_a) = cv2.threshold(A, a_threshold, 255, cv2.THRESH_BINARY)
        if arg_mode==0:
            thresh_weeds_a= imgProcess_tool.binarialization(A,0,arg_INV, a_threshold)
            thresh_weeds_b= imgProcess_tool.binarialization(B,0,arg_INV, b_threshold)
        elif arg_mode==1:
            thresh_weeds_b= imgProcess_tool.binarialization(B, 1, arg_INV)
            thresh_weeds_a= imgProcess_tool.binarialization(A, 1, arg_INV)
        elif arg_mode==2:
            thresh_weeds_b= imgProcess_tool.binarialization(B, 2, arg_INV)
            thresh_weeds_a= imgProcess_tool.binarialization(A, 2, arg_INV)
        cv2.imwrite('Debug/imgB_thr.jpg',thresh_weeds_b)
        cv2.imwrite('Debug/imgA_thr.jpg',thresh_weeds_a)
        imgRGB = cv2.merge([zeros, thresh_weeds_b, thresh_weeds_a])
        return thresh_weeds_a, thresh_weeds_b 
Example 42
Project: Farmbot_GeneralAP   Author: SpongeYao   File: class_ImageProcessing.py    (license) View Source Project 6 votes vote down vote up
def get_contour(self, arg_frame, arg_export_index, arg_export_path, arg_export_filename, arg_binaryMethod):
        # Otsu's thresholding after Gaussian filtering
        tmp = cv2.cvtColor(arg_frame, cv2.COLOR_RGB2GRAY)
        blur = cv2.GaussianBlur(tmp,(5,5),0)
        if arg_binaryMethod== 0:
            ret, thresholdedImg= cv2.threshold(blur.copy() , self.threshold_graylevel, 255 , 0)
        elif arg_binaryMethod == 1:
            ret,thresholdedImg = cv2.threshold(blur.copy(),0 ,255 ,cv2.THRESH_BINARY+cv2.THRESH_OTSU)
        elif arg_binaryMethod== 2:
            thresholdedImg = cv2.adaptiveThreshold(blur.copy(),255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY,5,0)

        result = cv2.cvtColor(thresholdedImg, cv2.COLOR_GRAY2RGB)
        ctrs, hier = cv2.findContours(thresholdedImg, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

        ctrs = filter(lambda x : cv2.contourArea(x) > self.threshold_size , ctrs)

        rects = [[cv2.boundingRect(ctr) , ctr] for ctr in ctrs]

        for rect , cntr in rects:
            cv2.drawContours(result, [cntr], 0, (0, 128, 255), 3)
        if arg_export_index:
            cv2.imwrite(arg_export_path+ arg_export_filename+'.jpg', result)
        print "Get Contour success"
        return result 
Example 43
Project: Hybrid-Artificial-Potential-Field-A-star-Planning   Author: vampcoder   File: A-star-potential-field-hybrid-type-1-controller.py    (license) View Source Project 6 votes vote down vote up
def classify(img):
    cimg = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    img2 = cv2.medianBlur(cimg, 13)

    ret, thresh1 = cv2.threshold(cimg, 100, 120, cv2.THRESH_BINARY)
    t2 = copy.copy(thresh1)

    x, y = thresh1.shape
    arr = np.zeros((x, y, 3), np.uint8)
    final_contours = []
    image, contours, hierarchy = cv2.findContours(t2, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
    #cv2.imshow('image', image)
    #k = cv2.waitKey(0)
    for i in range(len(contours)):
        cnt = contours[i]
        if cv2.contourArea(cnt) > 3000 and cv2.contourArea(cnt) < 25000:
            cv2.drawContours(img, [cnt], -1, [0, 255, 255])
            cv2.fillConvexPoly(arr, cnt, [255, 255, 255])
            final_contours.append(cnt)
    #cv2.imshow('arr', arr)
    #k = cv2.waitKey(0)
    return final_contours,arr 
Example 44
Project: image_text_reader   Author: yardstick17   File: remove_noise.py    (license) View Source Project 6 votes vote down vote up
def remove_noise_and_smooth(file_name):
    logging.info('Removing noise and smoothening image')
    img = cv2.imread(file_name, 0)
    filtered = cv2.adaptiveThreshold(img.astype(np.uint8), 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 41, 3)
    kernel = np.ones((1, 1), np.uint8)
    opening = cv2.morphologyEx(filtered, cv2.MORPH_OPEN, kernel)
    closing = cv2.morphologyEx(opening, cv2.MORPH_CLOSE, kernel)
    img = image_smoothening(img)
    or_image = cv2.bitwise_or(img, closing)
    return or_image 
Example 45
Project: Python_SelfLearning   Author: fukuit   File: binarize_allfiles.py    (license) View Source Project 6 votes vote down vote up
def convert_and_count(target, ext):
    for root, dirs, files in os.walktarget):
        for f in files:
            fname, fext = os.path.splitext(f)
            if fext == ext:
                img = cv2.imread(os.path.join(root, f), 0)
                blur = cv2.GaussianBlur(img, (5, 5), 0)
                ret, imgb = cv2.threshold(blur, 0, 255, cv2.THRESH_BINARY+cv2.THRESH_OTSU)
                cv2.imwrite(os.path.join(root, fname + ".bin.png"), imgb)
                cnt = 0
                for val in imgb.flat:
                    if val == 0:
                        cnt += 1
                ratio = cnt / img.size
                msg = "%s:\t%.5f" % (f, ratio)
                print(msg) 
Example 46
Project: WeiQiRecognition   Author: JDython   File: chess_detection.py    (license) View Source Project 6 votes vote down vote up
def img_pre_treatment(file_path):
    im = cv2.imread(file_path)
    resize_pic=cv2.resize(im,(640,480),interpolation=cv2.INTER_CUBIC)
    resize_pic = cv2.GaussianBlur(resize_pic,(5,5),0)
    cv2.imwrite('static/InterceptedIMG/resize.jpg',resize_pic)
    kernel = np.ones((3,3),np.uint8)
    resize_pic = cv2.erode(resize_pic,kernel,iterations = 3)
    resize_pic = cv2.dilate(resize_pic,kernel,iterations = 3)
    cv2.imshow('image',resize_pic)
    k = cv2.waitKey(0) & 0xFF
    if k == 27:
        cv2.destroyAllWindows()
    gray = cv2.cvtColor(resize_pic,cv2.COLOR_BGR2GRAY)
    ret, binary = cv2.threshold(gray,90,255,cv2.THRESH_BINARY)
    cv2.imshow('image',binary)
    k = cv2.waitKey(0) & 0xFF
    if k == 27:
        cv2.destroyAllWindows()
    return resize_pic,binary 
Example 47
Project: card-scanner   Author: RFVenter   File: scan2.py    (license) View Source Project 6 votes vote down vote up
def contrast_image(image, thresh1=180, thresh2=200, show=False):
	image = imutils.resize(image, height=scale_factor)
	# convert it to grayscale, and blur it slightly
	gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
	gray2 = cv2.GaussianBlur(gray, (5, 5), 0)

	# threshold the image, then perform a series of erosions + dilations to remove any small regions of noise
	thresh = cv2.threshold(gray2, thresh1, thresh2, cv2.THRESH_BINARY)[1]
	thresh2 = cv2.erode(thresh, None, iterations=2)
	thresh3 = cv2.dilate(thresh2, None, iterations=2)

	if show is True: #this is for debugging puposes
		cv2.imshow("Contrast", thresh3)
		cv2.waitKey(0)
		cv2.destroyAllWindows()

	return thresh 
Example 48
Project: card-scanner   Author: RFVenter   File: scan.py    (license) View Source Project 6 votes vote down vote up
def contrast_image(image, thresh1=180, thresh2=200, show=False):
	image = imutils.resize(image, height=scale_factor)
	# convert it to grayscale, and blur it slightly
	gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
	gray2 = cv2.GaussianBlur(gray, (5, 5), 0)

	# threshold the image, then perform a series of erosions + dilations to remove any small regions of noise
	thresh = cv2.threshold(gray2, thresh1, thresh2, cv2.THRESH_BINARY)[1]
	thresh2 = cv2.erode(thresh, None, iterations=2)
	thresh3 = cv2.dilate(thresh2, None, iterations=2)

	if show is True: #this is for debugging puposes
		cv2.imshow("Contrast", thresh3)
		cv2.waitKey(0)
		cv2.destroyAllWindows()

	return thresh 
Example 49
Project: card-scanner   Author: RFVenter   File: scan.py    (license) View Source Project 6 votes vote down vote up
def contrast_image(image, thresh1=180, thresh2=200, show=False):
	image = imutils.resize(image, height=scale_factor)
	# convert it to grayscale, and blur it slightly
	gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
	gray2 = cv2.GaussianBlur(gray, (5, 5), 0)

	# threshold the image, then perform a series of erosions + dilations to remove any small regions of noise
	thresh = cv2.threshold(gray2, thresh1, thresh2, cv2.THRESH_BINARY)[1]
	thresh2 = cv2.erode(thresh, None, iterations=2)
	thresh3 = cv2.dilate(thresh2, None, iterations=2)

	if show is True: #this is for debugging puposes
		cv2.imshow("Contrast", thresh3)
		cv2.waitKey(0)
		cv2.destroyAllWindows()

	return thresh 
Example 50
Project: intel-cervical-cancer   Author: wangg12   File: mask_GMM.py    (license) View Source Project 6 votes vote down vote up
def cropCircle(img):
    '''
    there many imaged taken thresholded, which means many images is
    present as a circle with black surrounded. This function is to
    find the largest inscribed rectangle to the thresholed image and
    then crop the image to the rectangle.

    input: img - the cv2 module

    return: img_crop, rectangle, tile_size
    '''
    if(img.shape[0] > img.shape[1]):
        tile_size = (int(img.shape[1]*256/img.shape[0]),256)
    else:
        tile_size = (256, int(img.shape[0]*256/img.shape[1]))

    img = cv2.resize(img, dsize=tile_size)

    gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY);
    _, thresh = cv2.threshold(gray, 10, 255, cv2.THRESH_BINARY)

    _, contours, _ = cv2.findContours(thresh.copy(),cv2.RETR_TREE,cv2.CHAIN_APPROX_NONE)

    main_contour = sorted(contours, key = cv2.contourArea, reverse = True)[0]

    ff = np.zeros((gray.shape[0],gray.shape[1]), 'uint8')
    cv2.drawContours(ff, main_contour, -1, 1, 15)
    ff_mask = np.zeros((gray.shape[0]+2,gray.shape[1]+2), 'uint8')
    cv2.floodFill(ff, ff_mask, (int(gray.shape[1]/2), int(gray.shape[0]/2)), 1)

    rect = maxRect(ff)
    rectangle = [min(rect[0],rect[2]), max(rect[0],rect[2]), min(rect[1],rect[3]), max(rect[1],rect[3])]
    img_crop = img[rectangle[0]:rectangle[1], rectangle[2]:rectangle[3]]
    cv2.rectangle(ff,(min(rect[1],rect[3]),min(rect[0],rect[2])),(max(rect[1],rect[3]),max(rect[0],rect[2])),3,2)

    return [img_crop, rectangle, tile_size]