我有一个房间的图像。我想检测图像中的边缘 - 即在天花板周围画一条线或在窗户周围说。问题有多难 - 有没有图书馆这样做?
这适用于iOS应用,但我的问题是平台/语言无关。
答案 0 :(得分:2)
这是一个名为edge.cpp的openCV源代码示例 http://opencv.willowgarage.com/wiki/ 构建和运行示例应用程序(/ sample dir)非常有趣
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <stdio.h>
using namespace cv;
using namespace std;
int edgeThresh = 1;
Mat image, gray, edge, cedge;
// define a trackbar callback
static void onTrackbar(int, void*)
{
blur(gray, edge, Size(3,3));
// Run the edge detector on grayscale
Canny(edge, edge, edgeThresh, edgeThresh*3, 3);
cedge = Scalar::all(0);
image.copyTo(cedge, edge);
imshow("Edge map", cedge);
}
static void help()
{
printf("\nThis sample demonstrates Canny edge detection\n"
"Call:\n"
" /.edge [image_name -- Default is fruits.jpg]\n\n");
}
const char* keys =
{
"{@image |fruits.jpg|input image name}"
};
int main( int argc, const char** argv )
{
help();
CommandLineParser parser(argc, argv, keys);
string filename = parser.get<string>(1);
image = imread(filename, 1);
if(image.empty())
{
printf("Cannot read image file: %s\n", filename.c_str());
help();
return -1;
}
cedge.create(image.size(), image.type());
cvtColor(image, gray, CV_BGR2GRAY);
// Create a window
namedWindow("Edge map", 1);
// create a toolbar
createTrackbar("Canny threshold", "Edge map", &edgeThresh, 100, onTrackbar);
// Show the image
onTrackbar(0, 0);
// Wait for a key stroke; the same function arranges events processing
waitKey(0);
return 0;
}
如果要将其与openCV构建分开构建,可以使用此脚本 (在c示例目录下提供了一个版本 - 我修改了它以编译额外的库)
#!/bin/sh
if [ $# -gt 0 ] ; then
base=`basename $1 .c`
echo "compiling $base"
gcc -ggdb `pkg-config opencv --cflags --libs` $base.c -o $base
else
for i in *.c; do
echo "compiling $i"
gcc -ggdb `pkg-config --cflags opencv` -o `basename $i .c` $i `pkg-config --libs opencv`;
done
for i in *.cpp; do
echo "compiling $i"
g++ -ggdb `pkg-config --cflags opencv` -o `basename $i .cpp` $i `pkg-config -- libs opencv` -lpthread -D_REENTRANT;
done
fi