像素操作
#include#include using namespace std; using namespace cv; int main(int argc, char**argv) { Mat src, src_gray; src= imread("b.jpg"); if (src.empty()) { cout << "could not load img.." << endl; return -1; } namedWindow("input", CV_WINDOW_AUTOSIZE); imshow("input", src); /* cvtColor(src, src_gray, CV_BGR2GRAY); namedWindow("output", CV_WINDOW_AUTOSIZE); imshow("output", src_gray); int height= src_gray.rows; int width = src_gray.cols; for (int row = 0; row < height; ++row) { for (int col = 0; col < width; ++col) { int gray = src_gray.at (row, col); src_gray.at */ Mat dst; dst.create(src.size(), src.type()); int height = src.rows; int width = src.cols; int chn = src.channels(); for(int row=0;row(row, col) = 255 - gray; } } imshow("output", src_gray); row) for (int col = 0; col < width; ++col) { if (chn == 1) { int gray = src_gray.at (row, col); src_gray.at (row, col) = 255 - gray; } else if(chn==3) { dst.at (row, col)[0] = 255 - src.at (row, col)[0]; dst.at (row, col)[1] = 255 - src.at (row, col)[1]; dst.at (row, col)[2] = 255 - src.at (row, col)[2]; } } //bitwise_not(src, dst); imshow("dst", dst); waitKey(0); return 0; }
直线,举行,圆形,椭圆的绘制与随机直线随机颜色的绘制
#include#include using namespace std; using namespace cv; Mat src; const char *drawdemo_win = "draw shapes and text demo"; void MyLines(); void MyRectangle(); void MyEclipse(); void MyCircle(); void MyPolygon(); void RandomLineDemo(); int main(int argc, char **argv) { src = imread("b.jpg"); if (src.empty()) { cout << "Load image failed" << endl; return -1; } imshow("input image", src); //MyLines(); //MyRectangle(); //MyEclipse(); //MyCircle(); //MyPolygon(); //MyPolygon(); //putText(src, "Hello OpenCV", Point(300, 300), CV_FONT_HERSHEY_COMPLEX, 1.0, Scalar(12, 255, 255), 3, 8); RandomLineDemo(); imshow("draw", src); waitKey(0); return 0; } void MyLines() { Point p1 = Point(20, 30); Point p2; p2.x = 400; p2.y = 400; Scalar color = Scalar(0, 0, 255); line(src, p1, p2, color, 1, LINE_AA); } void MyRectangle() { Rect rect = Rect(200, 100, 300, 300); Scalar color = Scalar(255, 0, 0); rectangle(src, rect, color, 2, LINE_8); } void MyEclipse() { Scalar color = Scalar(0, 255, 0); ellipse(src, Point(src.cols / 2, src.rows / 2), Size(src.cols / 4, src.rows / 8), 30, 0, 90, color, 2, LINE_8); } void MyCircle() { Scalar color = Scalar(0,255, 255); Point center = Point(src.cols / 2, src.rows / 2); circle(src, center, 150,color, 2, 8); } void MyPolygon() { Point pts[1][5]; pts[0][0] = Point(100, 100); pts[0][1] = Point(100, 200); pts[0][2] = Point(200, 200); pts[0][3] = Point(200, 100); pts[0][4] = Point(100, 100); const Point *ppts[] = { pts[0] }; int npt[] = { 5 }; Scalar color = Scalar(255, 13, 255); fillPoly(src, ppts, npt, 1, color, 8); } void RandomLineDemo() { RNG rng(1334); Point pt1; Point pt2; Mat bg = Mat::zeros(src.size(), src.type()); namedWindow("Random Line Demo", CV_WINDOW_AUTOSIZE); for(int i=0;i<1000000;++i) { pt1.x = rng.uniform(0, src.cols); pt2.x = rng.uniform(0, src.cols); pt1.y = rng.uniform(0, src.rows); pt2.y = rng.uniform(0, src.rows); Scalar color = Scalar(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255)); if (waitKey(50) > 0) break; line(bg, pt1, pt2, color, 1, 8); imshow("Random Line Demo", bg); } }