#include<opencv2\opencv.hpp>
#include<iostream>
using namespace cv;
using namespace std;
int main(int argc, char** argv)
{
Mat grayim(600, 800, CV_8UC1);
Mat colorim(600, 800, CV_8UC3);
#ifdef USINGAT
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;
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
MatIterator_<uchar> grayit,grayend;
for (grayit = grayim.begin<uchar>(), grayend = grayim.end<uchar>();
grayit != grayend; grayit++){
*grayit = rand() % 255;
}
MatIterator_<Vec3b> colorit, colorend;
for (colorit = colorim.begin<Vec3b>(), colorend = colorim.end<Vec3b>();
colorit != colorend; colorit++){
(*colorit)[0] = rand() % 255;
(*colorit)[1] = rand() % 255;
(*colorit)[2] = rand() % 255;
}
#endif
#ifdef USINGPOINTER
for (int i = 0; i < grayim.rows; i++){
uchar* p = grayim.ptr<uchar>(i);
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;
p[j][1] = j % 255;
p[j][2] = (i + j) % 255;
}
}
#endif
#ifdef SHOWIMG
imshow("grayim", grayim);
imshow("colorim", colorim);
waitKey(0);
#endif
#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;
}
}
Mat line;
for (int i = 0; i < colorim.rows; i++){
line = colorim.row(i);
colorim.row(i) = (2 * line);
}
Mat rect;
#endif
#ifdef hj
Mat A = Mat::eye(4, 4, CV_32SC1);
Mat B = A * 3 + 1;
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
#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;
}
}
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;
}
}
#endif
#define memofcv_
#ifdef memofcv
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
#ifdef trans
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);
#endif
#ifdef imrw
Mat myimg = imread("3.jpg", 0);
if (myimg.empty()){
printf_s("Can not open image\n");
return -1;
}
Mat result;
Canny(myimg, result, 50, 150);
imwrite("3_ex.png", result,std::vector<int,allocator<int>>(5));
imshow("s", myimg);
imshow("d", result);
waitKey(0);
#endif
#ifdef movier
VideoCapture cap("xx.mp4");
if (!cap.isOpened()){
printf_s("Can not open video\n");
return -1;
}
Mat edges,frame;
namedWindow("edges", 1);
while (true){
cap >> frame;
if (frame.empty()){
break;
}
imshow("edges", frame);
if (waitKey(10) >= 0)
break;
}
#endif
#ifdef moview
Size s(320, 240);
VideoWriter writer = VideoWriter("hj.avi", CV_FOURCC('M', 'J', 'P', 'G'), 25, s);
if (!writer.isOpened()){
printf_s("Can not create video\n");
return -1;
}
Mat frame(s, CV_8UC3);
for (int i = 100; i >=0; i--){
frame = Scalar::all(0);
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);
writer << frame;
}
VideoCapture cap("hj.avi");
if (!cap.isOpened()){
printf_s("Can not open video\n");
return -1;
}
Mat hj;
while (true){
cap >>hj;
if (hj.empty()){
break;
}
imshow("hj",hj);
if (waitKey(100) >= 0)
break;
}
#endif
exit(EXIT_SUCCESS);
}