如何检测连接图像元素的角点“关节”?

2024-05-23 21:13:44 发布

您现在位置:Python中文网/ 问答频道 /正文

我通过Python 3.7使用OpenCV。我有一组单色图像,看起来像这样:

enter image description here

我想找到这些图像上的所有“关节点”,其中“关节点”是两块木板的每个交点的中心(1像素)。这些“关节”大致由下图中的红色圆环表示:

enter image description here

第一个想法是对图像进行骨架化,然后通过算法找到所有连接的边缘,但所有骨架化技术都给了我扭曲或圆角以及额外的“萌芽”

import cv2
import numpy as np
from skimage.morphology import skeletonize

image = cv2.imread("SOURCE_IMAGE.jpg", cv2.IMREAD_GRAYSCALE)
binary_image = cv2.adaptiveThreshold(image, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY_INV, 91, 12)
skeleton = (skeletonize(binary_image//255) * 255).astype(np.uint8)

结果:

enter image description here

第二个想法是找到内部轮廓,将它们近似到边界点,找到最近的邻居,然后以某种方式计算中心,但是,同样,精明的边缘检测方法给了我扭曲的角落和额外的点

import cv2

image = cv2.imread("SOURCE_IMAGE.jpg", cv2.IMREAD_GRAYSCALE)
edged = cv2.Canny(image, 100, 200)

结果:

enter image description here

这个问题有可靠的解决方法吗


Tags: 图像imageimportsourcenpcv2边缘骨架
2条回答

这是我解决这个问题的方法:

  1. 确定垂直线
  2. 确定水平线
  3. 找出它们的交点,这些交点是关节

第一步,检查每一列,确定细线并使其变为黑色(0)。结果只会是垂直线。对于第二步,请执行相反的操作。 最后,将垂直线图像水平线图像进行比较。两者中的白色(255)像素是交点

<> > > EM>:EEM><强>请不要因为C++中的编码而责备我。我不熟悉python,我只是想展示我的方法和结果

以下是代码和结果:

资料来源:

enter image description here

垂直线:

enter image description here

水平线:

enter image description here

结果:

enter image description here

守则:

#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>
using namespace std;
using namespace cv;

int main()
{
    Mat img = imread("/ur/image/directory/joints.jpg",1);

    imshow("Source",img);

    int checker = 1,checker2 = 1;
    int begin_y,finish_y2,finish_y,begin_y2;
    Mat vertical_img = img.clone();
    Mat horizontal_img = img.clone();

    cvtColor(vertical_img,vertical_img,CV_BGR2GRAY);
    cvtColor(horizontal_img,horizontal_img,CV_BGR2GRAY);

    int finish_checker = 0,finish_checker2=0;
    for(int i=0;i<horizontal_img.rows;i++)
    {
        for(int j=0;j<horizontal_img.cols;j++)
        {
            if(horizontal_img.at<uchar>(Point(j,i))>100 && checker)
            {
                begin_y = j;
                checker = 0;
            }

            if(horizontal_img.at<uchar>(Point(j,i))<20 && checker==0)
            {
                finish_y = j;
                checker = 1;
                finish_checker = 1;

            }

            if(finish_checker)
            {
                if((finish_y-begin_y)<30)
                {
                    for(int h=begin_y-2;h<=finish_y;h++)
                    {
                        horizontal_img.at<uchar>(Point(h,i)) = 0;
                    }
                }

                finish_checker = 0;
            }
        }
    }

    imshow("Horizontal",horizontal_img);

    for(int i=0;i<vertical_img.cols;i++)
    {
        for(int j=0;j<vertical_img.rows;j++)
        {
            if(vertical_img.at<uchar>(Point(i,j))>100 && checker2)
            {
                begin_y2 = j;
                checker2 = 0;
            }
            if(vertical_img.at<uchar>(Point(i,j))<50 && checker2==0)
            {
                finish_y2 = j;
                checker2 = 1;
                finish_checker2 = 1;
            }
            if(finish_checker2)
            {
                if((finish_y2-begin_y2)<30)
                {
                    for(int h=begin_y2-2;h<=finish_y2;h++)
                    {
                        vertical_img.at<uchar>(Point(i,h)) = 0;
                    }
                }
                finish_checker2 = 0;
            }
        }
    }
    imshow("Vertical",vertical_img);

    for(int y=0;y<img.cols;y++)
    {
        for(int z=0;z<img.rows;z++)
        {
            if(vertical_img.at<uchar>(Point(y,z))>200 && horizontal_img.at<uchar>(Point(y,z))>200)
            {
                img.at<cv::Vec3b>(z,y)[0]=0;
                img.at<cv::Vec3b>(z,y)[1]=0;
                img.at<cv::Vec3b>(z,y)[2]=255;
            }
        }
    }

    imshow("Result",img);
    waitKey(0);
    return 0;
}

这里使用Python而不是C++的一个稍微修改的@YunusTemurlenk's方法。这个想法是:

  1. 获取二值图像。加载图像,转换为灰度,Gaussian blur,然后Otsu's threshold

  2. 获取水平和垂直线条掩码。使用^{}创建水平和垂直结构元素,然后执行^{}以隔离线条

  3. 找到关节。我们^{}将两个遮罩组合在一起以获得关节

  4. 在关节遮罩上找到质心。我们find contours然后计算centroid


水平/垂直线遮罩

检测到绿色接头

enter image description here

结果

enter image description here

import cv2
import numpy as np

# Load image, grayscale, Gaussian blur, Otsus threshold
image = cv2.imread('1.jpg')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray, (3,3), 0)
thresh = cv2.threshold(blur, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)[1]

# Find horizonal lines
horizontal_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (10,1))
horizontal = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, horizontal_kernel, iterations=2)

# Find vertical lines
vertical_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (1,10))
vertical = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, vertical_kernel, iterations=2)

# Find joints
joints = cv2.bitwise_and(horizontal, vertical)

# Find centroid of the joints
cnts = cv2.findContours(joints, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
for c in cnts:
    # Find centroid and draw center point
    M = cv2.moments(c)
    cx = int(M['m10']/M['m00'])
    cy = int(M['m01']/M['m00'])
    cv2.circle(image, (cx, cy), 3, (36,255,12), -1)

cv2.imshow('thresh', thresh)
cv2.imshow('horizontal', horizontal)
cv2.imshow('vertical', vertical)
cv2.imshow('joints', joints)
cv2.imshow('image', image)
cv2.waitKey()     

相关问题 更多 >