//#define USINGAT
//#define USINGITERATOR
//#define USINGPOINTER
//#define SHOWIMG
#include<opencv2\opencv.hpp>
#include<iostream>
using namespace cv;
using namespace std;
int main(int argc, char** argv)
{
/*
Mat mat(3, 2, CV_8UC3, Scalar(0, 0, 200));
cout << "mat=" << mat << endl;
Mat z = Mat::zeros(2, 2, CV_8UC2);
cout << "z=" << z << endl;
Mat o = Mat::eye(2, 2, CV_8UC3);
cout << "o=" << o << endl;
*/
Mat grayim(600, 800, CV_8UC1);
Mat colorim(600, 800, CV_8UC3);
#ifdef USINGAT
/*traversal all item,and assign the item*/
for (int i = 0; i < grayim.rows; i++){
for (int j = 0; j < grayim.cols; j++){
grayim.at<uchar>(i, j) = (i + j * 10) % 255;
}
}
Vec3b pixel;
/*traversal all pixel,and set the pix's value*/
for (int i = 0; i < colorim.rows; i++){
for (int j = 0; j < colorim.cols; j++){
pixel[0] = i % 255 & 10;
pixel[1] = j % 255 & 10;
pixel[2] = ((i + j) & 100) % 255;
colorim.at<Vec3b>(i, j) = pixel;
}
}
#endif
#ifdef USINGITERATOR
/*using iterator to traversal the image mat*/
MatIterator_<uchar> grayit,grayend;
for (grayit = grayim.begin<uchar>(), grayend = grayim.end<uchar>();
grayit != grayend; grayit++){
*grayit = rand() % 255;//this is a pointer..
}
MatIterator_<Vec3b> colorit, colorend;
for (colorit = colorim.begin<Vec3b>(), colorend = colorim.end<Vec3b>();
colorit != colorend; colorit++){
/*BGR*/
(*colorit)[0] = rand() % 255;
(*colorit)[1] = rand() % 255;
(*colorit)[2] = rand() % 255;
}
#endif
#ifdef USINGPOINTER
/*using the data pointer to traversal the img*/
for (int i = 0; i < grayim.rows; i++){
//get the pointer of i-th rows
uchar* p = grayim.ptr<uchar>(i);
//set this rows
for (int j = 0; j < grayim.cols; j++){
p[j] = (i + j) % 255;
}
}
for (int i = 0; i < colorim.rows; i++){
Vec3b* p = colorim.ptr<Vec3b>(i);
for (int j = 0; j < colorim.cols; j++){
p[j][0] = i % 255;//B
p[j][1] = j % 255;//G
p[j][2] = (i + j) % 255;//R
}
}
#endif
#ifdef SHOWIMG
//show the image
imshow("grayim", grayim);
imshow("colorim", colorim);
waitKey(0);
#endif
//#define pp
#ifdef pp
for (int i = 0; i < colorim.rows; i++){
Vec3b* p = colorim.ptr<Vec3b>(i);
for (int j = 0; j < colorim.cols; j++){
p[j][0] = (i) % 255;
p[j][1] = (j) % 255;
p[j][2] = (i + j*i) % 255;
}
}
//imshow("colorimage", colorim);
//change something
Mat line;
for (int i = 0; i < colorim.rows; i++){
line = colorim.row(i);
colorim.row(i) = (2 * line);
}
//imshow("change", colorim);
//waitKey(0);
/*get some area*/
Mat rect;
//rect = colorim(Range(1,20), Range(1, 30));
//imshow("rect", rect);
//waitKey(0);
/*get the area*/
//Mat roi(grayim, Rect(10, 10, 100, 200));
//imshow("im", roi);
//waitKey(0);
//Mat roi2 = grayim(Rect(10, 10, 200, 300));
//cout << colorim << endl;
#endif
//#define hj
#ifdef hj
Mat A = Mat::eye(4, 4, CV_32SC1);
Mat B = A * 3 + 1;
/*
diag is the daig of the mat
=0 means the main diag
>0 means the main-down diag
=1 means the item with main-down
<0 means the main-up diag
*/
Mat C = B.diag(0) + B.col(1);
cout << "A=" << A << endl;
cout << "B=" << B << endl;
cout << "C=" << C << endl;
cout << "C.*diag(B)=" << C.dot(B.diag(0)) << endl;
#endif
//#define _mat
#ifdef _mat
Vec3b* p;
for (int i = 0; i < colorim.rows; i++){
p = colorim.ptr<Vec3b>(i);
for (int j = 0; j < colorim.cols; j++){
p[j][0] = i % 255;
p[j][1] = j % 255;
p[j][2] = (i + j) % 255;
}
}
//show the image
//imshow("color", colorim);
//waitKey(0);
//using the Mat_
Mat_<Vec3b> mm = (Mat_<Vec3b>&)colorim;
for (int i = 0; i < mm.rows; i++){
Vec3b* pp = mm.ptr<Vec3b>(i);
for (int j = 0; j < mm.cols; j++){
double d = (double)((i + j) % 255);
mm(i, j) = d;
}
}
//imshow("hit", mm);
//waitKey(0);
#endif
#define memofcv_
#ifdef memofcv
/*
let the colorim's pixel range[0..255]
*/
Mat r = Mat(3, 2, CV_8UC3);
randu(r, Scalar::all(0), Scalar::all(255));
cout << "r(default) = " << "\n" << r << " , " << endl << endl;
cout << "r(matlab) = " << "\n" << format(r,1) << " , " << endl << endl;
cout << "r(csv) = " << "\n" << format(r,2) << " , " << endl << endl;
cout << "r(python) = " << "\n" << format(r,3) << " , " << endl << endl;
cout << "r(numpy) = " << "\n" << format(r,4) << " , " << endl << endl;
#endif
//#define trans
#ifdef trans
/*
trans Mat to IpLImage
*/
//Mat img(Size(320, 240), CV_8UC3);
//IplImage iplimg = img;//hehe,just assign is ok.
/*
trans Mat to CvMat
*/
//CvMat cvimg = img;//hehe,just assign is ok.
IplImage * iplimg = cvLoadImage("1.jpg");
Mat img = cvarrToMat(iplimg);
CvMat* cvimg = cvLoadImageM("2.jpg");
Mat img1 = cvarrToMat(cvimg);
imshow("cvarray", img);
imshow("cv2", img1);
waitKey(2000);//wait 2 seconds
#endif
//#define imrw
#ifdef imrw
/*
using imread and imwrite
imread(filename,flag)
flag>0 means return 3 chunnel image,and if not 3,trans to 3
flag=0 means return 1 chunnel image,and if not 1,trabs to 1
flag<0 means return the source image,not trans it.
imwrite(filename,image,params)
param->for image
jpeg:means the images's quility,0...100,95 is the default
png:means the level of compress,0..9,bigger means smaller 3 is default
ppm,pgm,pbm:text or binary?1 means binary,0 means text,1 is default
*/
//ok,read image to memory
Mat myimg = imread("3.jpg", 0);//0 means return 1 channel
//empty?
if (myimg.empty()){
printf_s("Can not open image\n");
return -1;
}
//Canny operator
Mat result;
Canny(myimg, result, 50, 150);
//store image
imwrite("3_ex.png", result,std::vector<int,allocator<int>>(5));
//show the image
imshow("s", myimg);
imshow("d", result);
waitKey(0);
#endif
//#define movier
#ifdef movier
/*
VideoCaptute and VideoWriter
read->read one frame
*/
//open video
VideoCapture cap("xx.mp4");
//check
if (!cap.isOpened()){
printf_s("Can not open video\n");
return -1;
}
Mat edges,frame;
//create the windows
namedWindow("edges", 1);
while (true){
//read frame
cap >> frame;
//check
if (frame.empty()){
break;
}
//cvtColor(frame, edges, CV_BGR2GRAY);
//Canny(edges, edges, 0, 30, 3);
imshow("edges", frame);
if (waitKey(10) >= 0)
break;
}
#endif
//#define moview
#ifdef moview
//define the video's size
Size s(320, 240);
//create the writer
VideoWriter writer = VideoWriter("hj.avi", CV_FOURCC('M', 'J', 'P', 'G'), 25, s);
//check
if (!writer.isOpened()){
printf_s("Can not create video\n");
return -1;
}
//video frame
Mat frame(s, CV_8UC3);
for (int i = 100; i >=0; i--){
//set the frame's color
frame = Scalar::all(0);
//get the number
char text[128];
sprintf_s(text, "%d", i);
putText(frame, text, Point(s.width / 3, s.height / 3),
FONT_HERSHEY_SCRIPT_SIMPLEX, 3,Scalar(0,0,255),3,8);
//write to video
writer << frame;
}
//open video
VideoCapture cap("hj.avi");
//check
if (!cap.isOpened()){
printf_s("Can not open video\n");
return -1;
}
Mat hj;
while (true){
//read frame
cap >>hj;
//check
if (hj.empty()){
break;
}
imshow("hj",hj);
if (waitKey(100) >= 0)
break;
}
#endif
exit(EXIT_SUCCESS);
}
opencv 入门代码
最新推荐文章于 2024-01-27 01:37:26 发布