8/28/2013

OpenCV Mat Data point access and memcpy to another Mat, example source code

This is example source code.

-----
Mat inImg = imread("Penguins.jpg",0);
//Data point copy
unsigned char * pData = inImg.data;
 
int width = inImg.rows;
int height = inImg.cols;
//gray color value reverse
for(int i=0; i < width; ++i)
{
  for(int j=0; j < height; ++j)
  {
   pData[j*width+i] = 255-pData[j*width+i];
  }
}
//create another Mat same size, type with inImg
Mat outImg(width, height, CV_8UC1);
//data copy using memcpy function
memcpy(outImg.data, pData, sizeof(unsigned char)*width*height);
 
//processing and copy check
namedWindow("Test");
imshow("Test", inImg);

namedWindow("Test2");
imshow("Test2", outImg);

cvWaitKey(0);
-----

Thank you.

8/26/2013

OpenCV - RotatedRect Draw Example source code

This source code is copied from this site -> http://docs.opencv.org/modules/core/doc/basic_structures.html for our convenience.


Mat image(200, 200, CV_8UC3, Scalar(0));
RotatedRect rRect = RotatedRect(Point2f(100,100), Size2f(100,50), 30);

Point2f vertices[4];
rRect.points(vertices);
for (int i = 0; i < 4; i++)
    line(image, vertices[i], vertices[(i+1)%4], Scalar(0,255,0));

Rect brect = rRect.boundingRect();
rectangle(image, brect, Scalar(255,0,0));

imshow("rectangles", image);
waitKey(0);



We can get rect information and rotatedRect information from RotatedRect variance.
From the above source code, green line is drawn by line function for describing rotated rect. 
And Blue line means outlier rect of target. It is drawn by rectangle function.
Thank you.

8/23/2013

OpenCV morphologyEx, getStructuringElement function example source


This is example source code.


Mat img;
img = imread("moSample.jpg");

Mat element = getStructuringElement(MORPH_RECT, Size(3, 3), Point(1,1) );
Mat rImg;
morphologyEx(img, rImg, CV_MOP_CLOSE, element);
//morphologyEx(img, img, CV_MOP_CLOSE, element); -> It is also ok.

namedWindow("i");
imshow("i",img);
namedWindow("r");
imshow("r",rImg);
cvWaitKey(0);


In here
We can set filter box size and center point using getStructuringElement function.
-first parameter has
MORPH_RECT, MORPH_CROSS, MORPH_ELLIPSE
They show the shape of the filter box.

-second parameter means size of box.
-third parameter means center point of the box.

This picture is result of morphology processing.
using 9x9 box size /  4,4 center



Thank you.

Visual studio 2012 + OpenCV 2.46 Setting method

First of all, download opencv 2.46 on this page -> http://opencv.org/downloads.html
The version 2.4.6 is latest version.


Extract in appropriate folder. " C:\OpenCV264\ " this path is suitable.

In VS2012 setting..
In the project properties, add lib and include path information.


Copy all dll files in the 'C:\opencv246\build\x86\vc11\bin' paste them to 'C:\Windows\system' . 

and sample source code

------------------------------------------------

#include < stdio.h >
#include < opencv2\opencv.hpp >


#ifdef _DEBUG
#pragma comment(lib, "opencv_core246d.lib") 
#pragma comment(lib, "opencv_imgproc246d.lib")   //MAT processing
//#pragma comment(lib, "opencv_objdetect246d.lib") 
//#pragma comment(lib, "opencv_gpu246d.lib")
//#pragma comment(lib, "opencv_features2d246d.lib")
#pragma comment(lib, "opencv_highgui246d.lib")
//#pragma comment(lib, "opencv_ml246d.lib")
#else
#pragma comment(lib, "opencv_core246.lib")
#pragma comment(lib, "opencv_imgproc246.lib")
//#pragma comment(lib, "opencv_objdetect246.lib")
//#pragma comment(lib, "opencv_gpu246.lib")
//#pragma comment(lib, "opencv_features2d246.lib")
#pragma comment(lib, "opencv_highgui246.lib")
//#pragma comment(lib, "opencv_ml246.lib")
#endif

using namespace cv;


void main()
{
 Mat img;
 img = imread("test.jpg");
 namedWindow("t");
 imshow("t",img);
 cvWaitKey(0);
}



------------------------------------------------------------

Thank you.