Study notes (28): a computer vision science that is understood (the first quarter) - gray threshold segmentation combat exercise

Learning immediately: https://edu.csdn.net/course/play/26281/327086?utm_source=blogtoedu

Edge-based segmentation contour:

On the basis of the edge detection, the result of closing edge based segmentation construct. Edge detection in the binarized image.

OpenCV related functions:

mode parameters:

CV.RETR_EXTERNAL: detect only the most outer profile , comprising an inner peripheral profile in the outer profile is ignored;

CV.RETR_LIST: detect all contours , including the inner circle, outer profile, but detected the outline does not establish a hierarchical relationship, independent of each other, there is no hierarchy, which means that this search mode parent or embedded profile does not exist profile , so that the third, fourth components are all elements of the vector is set to -1 hierarchy, it will be referred to specifically below;

CV.RETR_CCOMP: detect all the contours, but all the contours established only two hierarchical relationship, peripherals as the top, if the inner perimeter around the outline of the profile also contains other information, all within the contour of the inner circle are attributable to the top;

CV.RETR_TREE: detect all outline, outline all levels to establish a tree structure. The outer contour comprises an inner contour, the inner contour profile may also contain embedded continue.

 The method defined contour approximation method, parameters:

CV.CHAIN_APPROX_NONE: Save all successive contour points on the object boundary contours into the vector;

CV.CHAIN_APPROX_SIMPLE: save only the inflection point of the contour information , to save all the inflection point of the contour vector into the contours, the information points on the line segment will not be retained between the inflection point and the inflection point;

CV.CHAIN_APPROX_TC89_L1: teh-Chinl chain using approximation algorithm;

CV.CHAIN_APPROX_TC89_KCOS: teh-Chinl chain using approximation algorithm.

Thresholding:

cv.threshold(img, thresh, maxval, type[, dst])

Parameter Description:

img: the original image.

thresh: threshold for classification.

maxval: is above (below) the new value to the threshold value.

type: the method chosen, defined as follows:

 Here is the code for (ppt on findContours return value should be three, less image values):

import cv2 as cv
import matplotlib.pyplot as plt
import copy

filename="d:/Rice.png"
image = cv.imread(filename)
gray=cv.cvtColor(image, cv.COLOR_BGR2GRAY)
#使用大津算法灰度阈值化
thr,bw = cv.threshold(gray, 0, 0xff, cv.THRESH_OTSU)
print("threshold is ", thr)

#画出灰度直方图
plt.hist(gray.ravel(), 256, [0,256])
plt.show()

#预处理-形态学开运算
element = cv.getStructuringElement(cv.MORPH_CROSS, (3,3))
bw = cv.morphologyEx(bw, cv.MORPH_OPEN, element)
#拷贝一份
seg = copy.deepcopy(bw)
#计算轮廓
final, cnts, hier = cv.findContours(seg, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE)
count = 0
#遍历所有区域,并去除面积过小的
for i in range(len(cnts), 0, -1):
    c = cnts[i-1]
    area = cv.contourArea(c)
    if area < 10:
        continue;
    count = count + 1
    #print("blob", i, " : ", area)
    
    #区域画框并标记
    x,y,w,h = cv.boundingRect(c)
    cv.rectangle(image, (x,y), (x+w, y+h), (0,0,0xff), 1)
    cv.putText(image, str(count), (x,y), cv.FONT_HERSHEY_PLAIN, 0.5, (0, 0xff, 0))
print("米粒数量:", count)
cv.imshow("原图", image)
cv.imshow("阈值化图", bw)
cv.waitKey()
cv.destroyAllWindows()

The results are shown:

Published 65 original articles · won praise 34 · views 260 000 +

Guess you like

Origin blog.csdn.net/huanggang982/article/details/104956661