在图像上找到特定的点 [英] find specific points on an image

查看:118
本文介绍了在图像上找到特定的点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个解决难题的程序.我的尝试可与我测试的示例拼图很好地配合使用.现在,我试图为一个实际的难题做一个. 这个新拼图的拼图碎片实际上并没有合适的形状.

I am trying to make a program to solve a puzzle an. My attempts work good with sample puzzles I made to test. Now I am trying to make one for a actual puzzle. The puzzle pieces of this new puzzle don't really have a proper shape.

我设法使图像变成黑白,最后变成1和0的数组,其中1表示块,0表示背景.我想找到一种方法来识别这些碎片的四个角,峰和深度的坐标.

I managed to make the image into black and white and finally in to array of 1s and 0s where 1s indicate the piece and 0s background. I want to find a way to identify the coordinates of the 4 corners, peaks and depths of these pieces.

我试图计算靠近1的0的数量,以查看边框中的最大曲线.但是形状不够平滑,无法正常工作.

I tried to count the numbers of 0s near the 1s to see the maximum curves in the border. But the shapes are not smooth enough for it to work.

counter = np.zeros((lenX,lenY),dtype=int)
for i in range(lenX):
    for j in range(lenY):
        if img[i,j]==1:
            counter[i,j] = count_white(img,i,j,lenX,lenY)

print(counter)
tpath = os.getcwd()+"/test.jpg"
print(cv2.imwrite(tpath, Image))
print("saved at : ",tpath)
np.savetxt("test.csv", counter, delimiter=",")

def count_white(img,x,y,lenX,lenY):
    X = [x-1,x,x+1,x+1,x+1,x,x-1,x-1]
    Y = [y-1,y-1,y-1,y,y+1,y+1,y+1,y]
    count = 0
    for i in range(len(X)):
        if X[i] < lenX and Y[i] < lenY:
            if img[X[i],Y[i]] == 0:
                count=count+1
    return count

有任何建议,参考或想法吗?

Any suggestion, references or ideas?

推荐答案

抱歉,C ++代码适用于您的情况:

Sorry for the C++ code but it works for your case:

cv::Mat gray = cv::imread("Sq01a.png", cv::IMREAD_GRAYSCALE);

gray = 255 - gray;


cv::Mat bin;
cv::threshold(gray, bin, 1, 255, cv::THRESH_BINARY);

cv::Mat bigBin(2 * bin.rows, 2 * bin.cols, CV_8UC1, cv::Scalar(0));
bin.copyTo(cv::Mat(bigBin, cv::Rect(bin.cols / 2, bin.rows / 2, bin.cols, bin.rows)));


std::vector<std::vector<cv::Point> > contours;
cv::findContours(bigBin, contours, cv::RETR_EXTERNAL, cv::CHAIN_APPROX_NONE);

if (contours.size() > 0)
{
    std::vector<cv::Point> tmp = contours[0];
    const cv::Point* elementPoints[1] = { &tmp[0] };
    int numberOfPoints = (int)tmp.size();
    cv::fillPoly(bigBin, elementPoints, &numberOfPoints, 1, cv::Scalar(255, 255, 255), 8);
}

int maxCorners = 20;
double qualityLevel = 0.01;
double minDistance = bigBin.cols / 8;
int blockSize = 5;
bool useHarrisDetector = true;
double k = 0.04;
std::vector<cv::Point2f> corners;
cv::goodFeaturesToTrack(bigBin, corners, maxCorners, qualityLevel, minDistance, cv::noArray(), blockSize, useHarrisDetector, k);

std::vector<cv::Point2f> resCorners;
std::vector<cv::Point2f> imgCorners = { cv::Point2f(0, 0), cv::Point2f(bigBin.cols, 0), cv::Point2f(bigBin.cols, bigBin.rows), cv::Point2f(0, bigBin.rows) };
for (auto imgCorn : imgCorners)
{
    size_t best_i = corners.size();
    float min_dist = bigBin.cols * bigBin.rows;
    for (size_t i = 0; i < corners.size(); ++i)
    {
        float dist = cv::norm(imgCorn - corners[i]);
        if (dist < min_dist)
        {
            best_i = i;
            min_dist = dist;
        }
    }
    if (best_i != corners.size())
    {
        resCorners.push_back(corners[best_i]);
    }
}
cv::Mat bigColor;
cv::cvtColor(bigBin, bigColor, cv::COLOR_GRAY2BGR);
for (auto corner : corners)
{
    cv::circle(bigColor, corner, 10, cv::Scalar(0, 0, 255.), 1);
}
for (auto corner : resCorners)
{
    cv::circle(bigColor, corner, 5, cv::Scalar(0, 255, 0), 2);
}

cv::imshow("gray", gray);
cv::imshow("bigColor", bigColor);
cv::waitKey(0);

这里是红色圆圈-哈里斯(Harris)的角和绿色-与图像角点最近的角.可以吗?

Here red circles - corners from Harris and green - the nearest to the image corners. It's OK?

这篇关于在图像上找到特定的点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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