查找凹形弯曲斑点的宽度和高度 [英] Finding width and height of concave curved shaped blob

查看:92
本文介绍了查找凹形弯曲斑点的宽度和高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在为计算像这样的图形的宽度和高度测量值这个问题scratch之以鼻.主要的挑战是我不能使用miBoundingrectangle,也无法找到一种从内部进行边界的方法,无论哪种方式,我都会丢失一些用于高度和宽度测量的像素.

I have been scratching my head over this problem of calculating the width and height measurements of a figure like this. The major challenge is I cant use miBoundingrectangle and cant figure out a way for bounding from inside, either way I'll lose some pixels for height and width measurement.

样本输入:

样本输出:

有没有防故障方法来进行精确的尺寸测量?

Is there any fail proof way to get an accurate dimension measurement?

以下是我正在尝试找到内部边界最大矩形的解决方案.

Below is a solution i was trying out to find inner bounding max rectangle.

_,contour2,_=cv2.findContours(im,cv2.RETR_CCOMP,cv2.CHAIN_APPROX_NONE)
for c in contour2:
    area=cv2.contourArea(c)
    if area ==25224.0:
        print(area)
        n = np.squeeze(contour2[0])

        x = sorted(n, key=lambda a:a[0])
        left = x[0]
        right = x[-1]
        print("",left,right)
        y= sorted(n, key=lambda a:a[1])
        top = y[0]
        bottom = y[-1]
        cv2.drawContours(im,[c],-1,(128,128,128),2)
        cv2.circle(im, (left[0],left[1]), 4, (128,128,128), 8)
        cv2.circle(im, (right[0],right[1]), 4, (128,128,128), 8)
        cv2.circle(im, (top[0],top[1]), 4, (128,128,128), 8)
        cv2.circle(im, (bottom[0],bottom[1]), 4, (128,128,128), 8)

        roi_w = int(np.sqrt((top[0]-right[0])*(top[0]-right[0])(top[1]-right[1])*(top[1]-right[1])))
        roi_h = int(np.sqrt((top[0]-left[0])*(top[0]-left[0])+(top[1]-left[1])*(top[1]-left[1])))
                pts1 = np.float32([top,right,left])

        new_top = top
        new_right = [top[0] + roi_w, top[1]]
        new_left = [top[0], top[1] + roi_h]
        pts2 = np.float32([new_top,new_right,new_left])

     cv2.imshow("threshed", im)`

推荐答案

这是OpenCV解决方案.主要思想是

Here's an OpenCV solution. The main idea is

  • 将图像转换为灰度并反转图像
  • 找到斑点的轮廓和质心
  • 使用Numpy切片抓取所有行/列像素
  • 计算非零像素以确定宽度/高度

我们将图像转换为灰度并反转.由于期望的ROI为白色,因此这是我们要依靠像素的图像.在这里,我们使用 cv2.moments()找到斑点的轮廓和中心坐标.这给了我们质心(即对象的中心(x,y)坐标)

We convert the image to grayscale and invert it. This is the image we will count pixels on since the desired ROI is in white. From here, we find the contour of the blob and the center coordinates using cv2.moments(). This gives us the centroid (i.e., the center (x, y)-coordinates of the object)

M = cv2.moments(c)
cX = int(M["m10"] / M["m00"])
cY = int(M["m01"] / M["m00"])

接下来,我们使用Numpy切片来获取所有行和列像素.我们使用cv2.countNonZero()来找到这样的行/列的宽度/高度

Next we use Numpy slicing to obtain all the row and column pixels. We use cv2.countNonZero() to find the width/height of the row/column like this

row_pixels = cv2.countNonZero(gray[cY][:])
column_pixels = cv2.countNonZero(gray[:, cX])

这是可视化

这是结果

第150行

row 150

第354列

import cv2
import numpy as np

image = cv2.imread('1.png')
inverted = 255 - image.copy()
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
gray = 255 - gray

cnts = cv2.findContours(gray, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]

for c in cnts:
    M = cv2.moments(c)
    cX = int(M["m10"] / M["m00"])
    cY = int(M["m01"] / M["m00"])

    cv2.circle(inverted, (cX, cY), 5, (36, 255, 12), -1)
    inverted[cY][:] = (36, 255, 12)
    inverted[:, cX] = (36, 255, 12)
    row_pixels = cv2.countNonZero(gray[cY][:])
    column_pixels = cv2.countNonZero(gray[:, cX])

print('row', row_pixels)
print('column', column_pixels)
cv2.imshow('inverted', inverted)
cv2.imwrite('inverted.png', image)
cv2.waitKey(0)

这篇关于查找凹形弯曲斑点的宽度和高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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