【问题标题】:Finding the corners of a puzzle piece寻找拼图的角落
【发布时间】:2015-02-24 08:58:12
【问题描述】:

场景:

扫描一个谜题,让计算机解决它(Java 应用程序)。

我正在寻找一种方法来找到拼图的角。

我不知道我是否可以使用框架来解决这个问题。我可以在这里使用模板之类的东西来找到角落吗?还是我必须自己实现一个算法?

我需要将单件放在笔直的位置。如果我有两个角,我可以计算将图像旋转到直线水平所需的角度。 我在特定的框架中不需要这样的函数。

(可选) 我的方法是: 自己实现一个算法(可能性能极差)。

  1. 创建一个二维数组,其中所有白色像素分配为 0,所有彩色/黑色像素分配为 1。
  2. 循环遍历二维数组并搜索像素为 1 的像素。从这里开始,搜索所有相邻的 1。
  3. 如果相邻的 1 少于 50 个,则忽略此“像素”并继续(扫描错误)
  4. 如果相邻的 1 超过 50 个,则为拼图(且无扫描错误)。完成这幅作品并将它们全部复制到一个新的 2D 阵列中,其中不应出现扫描错误。
  5. 在新数组中,搜索角度...

【问题讨论】:

  • 友情推荐——使用像OpenCV这样的图像处理库,而不是重新发明轮子
  • @Itay:谢谢,但这是我的问题的一部分。您认为使用 OpenCV 可以轻松解决这个问题吗?
  • 是的,当然。解决方案本身取决于您的作品的特征(如果角正好是 90 度等)。可以看this article的第三章(3个识别面)
  • @Itay:谢谢! :) 我一有时间就读。

标签: opencv image-processing bufferedimage javacv imagej


【解决方案1】:

我不是 OpenCV 专家,但我尝试过使用 Harris Corner Detector,如下所示:

////////////////////////////////////////////////////////////////////////////////
// Compile and run under OSX with:
// clang++ -O3 $(pkg-config --cflags --libs opencv) harris.cpp -o harris && ./harris
////////////////////////////////////////////////////////////////////////////////
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
#include <stdio.h>
#include <stdlib.h>

using namespace cv;
using namespace std;

int thresh = 200;

int main( )
{
    Mat src,gray;
    // Load image and convert to greyscale
    src=imread("jigsaw.jpg",1);

    cvtColor(src,gray,CV_BGR2GRAY);
    Mat dst,dst_norm,dst_norm_scaled;
    dst=Mat::zeros(src.size(),CV_32FC1);

    // Detect corners
    cornerHarris(gray,dst,7,5,0.05,BORDER_DEFAULT);

    // Normalize
    normalize(dst,dst_norm,0,255,NORM_MINMAX,CV_32FC1,Mat());
    convertScaleAbs(dst_norm,dst_norm_scaled);

    // Draw circles around corners
    for(int j = 0;j<dst_norm.rows;j++){
       for(int i = 0;i<dst_norm.cols;i++){
          if((int)dst_norm.at<float>(j,i)>thresh){
             circle(dst_norm_scaled,Point(i,j),10,Scalar(255,255,255),1,8,0);
          }
       }
    }

    // Display result
   //imwrite("result.png",dst_norm_scaled);
   namedWindow("corners_window",CV_WINDOW_AUTOSIZE);
   imshow("corners_window",dst_norm_scaled);

    waitKey(0);
    return(0);
}

并在确定的角落周围画了一些圆圈以供娱乐。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-06
    • 1970-01-01
    • 2012-11-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多