23条回答 默认 最新
- 无厘头编程 2020-12-27 09:26关注
Python 结果:
from imutils import contours from skimage import measure import numpy as np import imutils import cv2 as cv # read local file image = cv.imread('D:\OpenCV\image\led_light.png') # convert to grayscale gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY) blurred = cv.GaussianBlur(gray, (11, 11), 0) # reveal the brightest regions thresh = cv.threshold(blurred, 200, 255, cv.THRESH_BINARY)[1] # remove small spots # perform a series of erosions and dilations to remove # any small blobs of noise from the thresholded image thresh = cv.erode(thresh, None, iterations=2) thresh = cv.dilate(thresh, None, iterations=4) labels = measure.label(thresh) mask = np.zeros(thresh.shape, dtype="uint8") # loop over the unique components for label in np.unique(labels): # if this is the background label, ignore it if label == 0: continue # otherwise, construct the label mask and count the # number of pixels labelMask = np.zeros(thresh.shape, dtype="uint8") labelMask[labels == label] = 255 numPixels = cv.countNonZero(labelMask) # if the number of pixels in the component is sufficiently # large, then add it to our mask of "large blobs" if numPixels > 500: mask = cv.add(mask, labelMask) # find the contours in the mask, then sort them from left to # right cnts = cv.findContours(mask.copy(), cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE) cnts = imutils.grab_contours(cnts) cnts = contours.sort_contours(cnts)[0] # loop over the contours for (i, c) in enumerate(cnts): # draw the bright spot on the image (x, y, w, h) = cv.boundingRect(c) ((cX, cY), radius) = cv.minEnclosingCircle(c) cv.circle(image, (int(cX), int(cY)), int(radius), (0, 0, 255), 3) cv.putText(image, "#{}".format(i + 1), (x, y - 15), cv.FONT_HERSHEY_SIMPLEX, 0.45, (0, 0, 255), 2) # show the output image #cv.imshow('LED Light', thresh) cv.imshow('spot', image) cv.waitKey(0)
解决 4无用
悬赏问题
- ¥15 使用vofa上位机justfloat协议,有数据采集,但是没有波形
- ¥15 将两文件中相同行数行 打印至同一行
- ¥15 关于#单片机#的问题:c51单片机想做个密码重置按键K13不知道如何去做,用了24c02来存储
- ¥20 有没有会python的帮帮我😥
- ¥15 如何修改系统里的设备信息及某某镇某某村如图所示
- ¥15 变数的长度不一样”。是什么原因呢?如何修改?
- ¥15 matlab作业不会写
- ¥15 eclipse mqtt 消息消费问题
- ¥15 vue2(标签-chrome|关键词-浏览器兼容)
- ¥15 python网络流自动生成系统 医学领域