红点坐标检测 [英] Red dot coordinates detection

查看:189
本文介绍了红点坐标检测的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用红点跟踪零件的运动.之前我曾尝试使用白点和阈值,但是我正在使用的智能手机反射太多.计划是将一个点识别为轮廓,找到中心并用所有轮廓中心的坐标填充数组,以供进一步计算.

I'm trying to follow movement of a part using red dots. I tried with white dots and thresholding before, but there is too much reflection from the smartphone I'm using. The plan is to recognize a dot as a contour, find the center and fill the array with the coordinates of all contour centers for further calculation.

该代码在下面发布,它可以识别正确的点数,但是我得到除以零的错误.有人知道我在做什么错吗?

The code is posted bellow, it recognizes the correct number of dots, but I get the division by zero error. Does anyone know what I'm doing wrong?

图片: https://imgur.com/a/GLXGCPP

import cv2
import numpy as np
from matplotlib import pyplot as plt
import imutils

#load image
img = cv2.imread('dot4_red.jpg')

#apply median blur, 15 means it's smoothing image 15x15 pixels
blur = cv2.medianBlur(img,15)

#convert to hsv
hsv = cv2.cvtColor(blur, cv2.COLOR_BGR2HSV)

#color definition
red_lower = np.array([0,0,240])
red_upper = np.array([10,10,255])

#red color mask (sort of thresholding, actually segmentation)
mask = cv2.inRange(hsv, red_lower, red_upper)

#copy image for, .findContours distorts the source image
mask_copy = mask.copy()

#find contours
cnts = cv2.findContours(mask_copy,cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE)

#extract contours from the list??
cnts = imutils.grab_contours(cnts)

#count number of conoturs of specific size
s1 = 500
s2 = 10000
xcnts = []
for cnt in cnts:
    if s1<cv2.contourArea(cnt)<s2:
        xcnts.append(cnt)

n = len(xcnts)

#pre-allocate array for extraction of centers of contours
s = (n,2)
array = np.zeros(s)

#fill array of center coordinates 
for i in range(0,n):
    cnt = cnts[i]
    moment = cv2.moments(cnt)
    c_x = int(moment["m10"]/moment["m00"])
    c_y = int(moment["m01"]/moment["m00"])

    array[i,:] = [c_x, c_y]

#display image
cv2.namedWindow('image', cv2.WINDOW_NORMAL)
cv2.imshow('image', mask)
cv2.waitKey(0) & 0xFF
cv2.destroyAllWindows()

#print results
print ('number of dots, should be 4:',n)
print ('array of dot center coordinates:',array)

推荐答案

问题是颜色范围错误.因此,圆圈的蒙版中有孔.由于被零除. M00.您可以选择正确的颜色范围,也可以预先填充蒙版中的孔.或使用以下代码:

The problem was the wrong color range. Because of this, there were holes in the mask of the circles. Due to division by zero. M00. You can choose the correct color range or pre-fill the holes in the mask. Or use this code:

import cv2
import numpy as np
#load image
img = cv2.imread('ZayrMep.jpg')

#apply median blur, 15 means it's smoothing image 15x15 pixels
blur = cv2.medianBlur(img,15)

#convert to hsv
hsv = cv2.cvtColor(blur, cv2.COLOR_BGR2HSV)

#color definition
red_lower = np.array([160,210,230])
red_upper = np.array([180,255,255])

#red color mask (sort of thresholding, actually segmentation)
mask = cv2.inRange(hsv, red_lower, red_upper)

connectivity = 4  
# Perform the operation
output = cv2.connectedComponentsWithStats(mask, connectivity, cv2.CV_32S)
# Get the results

num_labels = output[0]-1

centroids = output[3][1:]
#print results
print ('number of dots, should be 4:',num_labels )
print ('array of dot center coordinates:',centroids)

这篇关于红点坐标检测的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆