Programowanie w systemie UNIX/OpenCV: Różnice pomiędzy wersjami

Usunięta treść Dodana treść
Linia 180:
 
./DisplayImage lena.jpg
 
==Gradient==
 
<source lang=cpp>
// original code by http://stackoverflow.com/users/951860/mevatron
// see http://stackoverflow.com/a/11157426/15485
// http://stackoverflow.com/users/15485/uvts-cvs added the code for saving x and y gradient component
 
#include <opencv2/opencv.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
 
#include <iostream>
#include <vector>
 
using namespace cv;
using namespace std;
 
Mat mat2gray(const cv::Mat& src)
{
Mat dst;
normalize(src, dst, 0.0, 255.0, cv::NORM_MINMAX, CV_8U);
 
return dst;
}
 
Mat orientationMap(const cv::Mat& mag, const cv::Mat& ori, double thresh = 1.0)
{
Mat oriMap = Mat::zeros(ori.size(), CV_8UC3);
Vec3b red(0, 0, 255);
Vec3b cyan(255, 255, 0);
Vec3b green(0, 255, 0);
Vec3b yellow(0, 255, 255);
for(int i = 0; i < mag.rows*mag.cols; i++)
{
float* magPixel = reinterpret_cast<float*>(mag.data + i*sizeof(float));
if(*magPixel > thresh)
{
float* oriPixel = reinterpret_cast<float*>(ori.data + i*sizeof(float));
Vec3b* mapPixel = reinterpret_cast<Vec3b*>(oriMap.data + i*3*sizeof(char));
if(*oriPixel < 90.0)
*mapPixel = red;
else if(*oriPixel >= 90.0 && *oriPixel < 180.0)
*mapPixel = cyan;
else if(*oriPixel >= 180.0 && *oriPixel < 270.0)
*mapPixel = green;
else if(*oriPixel >= 270.0 && *oriPixel < 360.0)
*mapPixel = yellow;
}
}
 
return oriMap;
}
 
int main(int argc, char* argv[])
{
Mat image = Mat::zeros(Size(320, 240), CV_8UC1);
circle(image, Point(160, 120), 80, Scalar(255, 255, 255), -1, CV_AA);
 
imshow("original", image);
 
Mat Sx;
Sobel(image, Sx, CV_32F, 1, 0, 3);
 
Mat Sy;
Sobel(image, Sy, CV_32F, 0, 1, 3);
 
Mat mag, ori;
magnitude(Sx, Sy, mag);
phase(Sx, Sy, ori, true);
 
Mat oriMap = orientationMap(mag, ori, 1.0);
 
imshow("x", mat2gray(Sx));
imshow("y", mat2gray(Sy));
 
imwrite("hor.png",mat2gray(Sx));
imwrite("ver.png",mat2gray(Sy));
 
imshow("magnitude", mat2gray(mag));
imshow("orientation", mat2gray(ori));
imshow("orientation map", oriMap);
waitKey();
 
return 0;
}
</source>
 
 
CMakeLists.txt :
<source lang=make>
cmake_minimum_required(VERSION 2.8)
project( g )
find_package( OpenCV REQUIRED )
include_directories( ${OpenCV_INCLUDE_DIRS} )
add_executable( g g.cpp )
target_link_libraries( g ${OpenCV_LIBS} )
</source>
 
Kompilacja :
cmake .
make
 
Uruchomienie
 
./g
 
=pomoc=