OpenCV汽车识别检测数量统计

Python+OpenCV汽车识别检测数量统计

如需远程调试,可加QQ905733049由专业技术人员远程协助!

运行代码如下:

import cv2
import numpy as np
img=cv2.imread("car1.jpg")
xsize,ysize,channel=img.shape

img=cv2.resize(img,(ysize//2,xsize//2))
frame=cv2.cvtColor(img,cv2.COLOR_BGR2HSV)
mask_blue=cv2.inRange(frame,lower_blue,upper_blue)
res_blue=cv2.bitwise_and(frame,frame,mask=mask_blue)
cv2.imshow("mask_blue",mask_blue)
cv2.imshow("res_blue",res_blue)

# blur and threshold the image
blurred = cv2.blur(mask_blue, (9, 9))
(_, thresh) = cv2.threshold(blurred, 90, 255, cv2.THRESH)

"""
(_, thresh) = cv2.threshold(mask_blue, 90, 255, cv2.THRESH)
"""

# perform a series of erosions and dilations
closed = cv2.erode(closed, None, iterations=4)
closed = cv2.dilate(closed, None, iterations=4)

cv2.imshow("mask_blue2",closed)
cv2.imwrite("mask_blue2.jpg",closed)

ret, thresh = cv2.threshold(closed, 50, 250, cv2.THRESH_BINARY)
thresh_not = cv2.bitwise_not(thresh)#二值图像的补集

kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3,3))


for i in range(550):
    F_dilation = cv2.dilate(F, kernel, iterations=1)
    F = cv2.bitwise_and(F_dilation, thresh_not)

    result = cv2.bitwise_not(F)

result,contours, hierarchy = cv2.findContours(result,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE) 
cv2.drawContours(result,contours,-1,(120,0,0),2) 

count=0 
ares_avrg=0  

for cont in contours:

    ares = cv2.contourArea(cont)

    if ares<50:  
        continue
    count+=1    
    ares_avrg+=ares

    print("{}-blob:{}".format(count,ares),end="  ") 

    rect = cv2.boundingRect(cont) 
    print("x:{} y:{}".format(rect[0],rect[1]))
    x={format(rect[0])}
    y={format(rect[1])}
    print(x)
    print(y)
    y=10 if rect[1]<10 else rect[1] 
    cv2.putText(img,str(count), cv2.FONT_HERSHEY_COMPLEX, 1, (255, 255, 255), 2) 

print(":{}".format(round(ares_avrg/ares,2))) 

cv2.namedWindow("DetectedPhoto", 0);
cv2.imshow('DetectedPhoto',result)






运行结果:

  • 3
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
红细胞识别数量统计系统可以使用OpenCV库进行开发。下面是一个简单的流程: 1. 读取图像:使用OpenCV中的imread函数读取红细胞图像。 2. 预处理:对图像进行预处理,包括灰度化、二值化、平滑处理等。 3. 分割红细胞:使用形态学操作(如膨胀和腐蚀)和轮廓检测等方法,将红细胞分割出来。 4. 特征提取:从每个红细胞中提取特征,例如面积、周长、形状等。 5. 数量统计:根据红细胞的特征,统计红细胞数量。 下面是一个简单的红细胞识别数量统计的示例代码: ``` import cv2 import numpy as np # 读取图像 img = cv2.imread('red_blood_cells.jpg') # 灰度化 gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # 二值化 ret, thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU) # 去除噪声 kernel = np.ones((3,3), np.uint8) opening = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, kernel, iterations=2) # 膨胀操作 sure_bg = cv2.dilate(opening,kernel,iterations=3) # 腐蚀操作 sure_fg = cv2.erode(sure_bg,kernel,iterations=2) # 寻找轮廓 contours, hierarchy = cv2.findContours(sure_fg,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE) # 绘制轮廓 cv2.drawContours(img, contours, -1, (0,0,255), 3) # 统计红细胞数量 print("Number of red blood cells: ", len(contours)) # 显示结果 cv2.imshow('result', img) cv2.waitKey(0) cv2.destroyAllWindows() ``` 这段代码可以读取一张红细胞图像,对其进行预处理和分割,然后统计红细胞数量并显示结果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

荷塘月色2

您的鼓励将是我创作最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值