open_cv_computer_vision_part1
// create image
cv:Mat function(){
cv::Mat ima(500,500,CV_8U,50);
return ima;
}
// create a new image made of 240 rows and 320 columns
cv::Mat image1(240,320,CV_8U,100);
// or:
cv::Mat image1(240,320,CV_8U,cv::Scalar(100));
// re-allocate a new image
// (only if size or type are different)
image1.create(200,200,CV_8U);
image1= 200;
cv::imshow("Image", image1); // show the image
// create a red color image
// channel order is BGR
cv::Mat image2(240,320,CV_8UC3,cv::Scalar(0,0,255));
// or:
cv::Mat image2(cv::Size(320,240),CV_8UC3);
image2= cv::Scalar(0,0,255);
cv::imshow("Image", image2); // show the image
// read an image
cv::Mat image3= cv::imread("puppy.bmp");
// all these images point to the same data block
cv::Mat image4(image3);
image1= image3;
// these images are new copies of the source image
image3.copyTo(image2);
cv::Mat image5= image3.clone();
// transform the image for testing
cv::flip(image3,image3,1);
// get a gray-level image from a function
cv::Mat gray= function();
// read the image in gray scale
image1= cv::imread("puppy.bmp", CV_LOAD_IMAGE_GRAYSCALE);
// convert the image into a floating point image [0,1]
image1.convertTo(image2,CV_32F,1/255.0,0.0);
cv::imshow("Image", image2); // show the image
// Test cv::Matx
// a 3x3 matrix of double-precision
cv::Matx33d matrix(3.0, 2.0, 1.0,
2.0, 1.0, 3.0,
1.0, 2.0, 3.0);
// a 3x1 matrix (a vector)
cv::Matx31d vector(5.0, 1.0, 3.0);
// multiplication
cv::Matx31d result = matrix*vector;
logo
// define an image window
cv::namedWindow("Image");
// read the image
cv::Mat image= cv::imread("puppy.bmp");
// read the logo
cv::Mat logo= cv::imread("smalllogo.png");
// define image ROI at image bottom-right
cv::Mat imageROI(image,
cv::Rect(image.cols-logo.cols, //ROI coordinates
image.rows-logo.rows,
logo.cols,logo.rows));// ROI size
// insert logo
logo.copyTo(imageROI);
cv::imshow("Image", image); // show the image
// re-read the original image
image= cv::imread("puppy.bmp");
// define image ROI at image bottom-right
imageROI= image(cv::Rect(image.cols-logo.cols,image.rows-logo.rows,
logo.cols,logo.rows));
// or using ranges:
imageROI= image(cv::Range(image.rows-logo.rows,image.rows),
cv::Range(image.cols-logo.cols,image.cols));
// use the logo as a mask (must be gray-level)
cv::Mat mask(logo);
// insert by copying only at locations of non-zero mask
logo.copyTo(imageROI,mask);
cv::Mat image;
image.rows;
image.cols;
image = cv::imread('puppy.bmp',cv::IMREAD_GRAYSCALE);
image.empty();//是否为空
image.channels();
cv::namedWindow('Original Image');
cv::imshow('Original Image',image);
cv::setMouseCallback('Original Image', onMouse, reinterpret_cast<void*>(&image));
cv::Mat result:
cv::flip(image,result,1);
cv::namedWindow("Output Image");
cv::imshow("Output Image", result);
cv::imwrite("output.bmp",result);
cv::circle(image, //destination image
cv::Point(155,110),) //center coordinate
65, //radius
0, //color
3,) //thickness
cv::putText(image, //destination image
"this is a dog.", //test
cv::Point(40,200), //text position
cv::FONT_HERSHEY_PLAIN, //font type
2.0, //font scale
255, //text color
2) //text thickness

浙公网安备 33010602011771号