opencv2遍历图像程序

来源:互联网 发布:淘口令转换软件 编辑:程序博客网 时间:2024/04/28 22:10

(1)program_one
#include "cv.h"#include "highgui.h"#include "cxcore.h"#include <iostream>using namespace std;using namespace cv;void colorReduce(const Mat &Image, Mat &result, int div = 64);int main(void){Mat scr_Image = imread("F:\\1\\yellow_lane\\0.png", 1);//int div;//for(int j = 0; j<ScrImage.rows; j++)//{//uchar* data = ScrImage.ptr<uchar>(j);//for (int i = 0; i<(ScrImage.cols * ScrImage.channels()); i++)//{////data[i] = data[i]/div*div + div/2;////*data++ = *data/div*div + div/2;//uchar mask = 0xf0;//div = 16;//data[i] = (data[i] & mask) + div/2;//}//}Mat dst_Image;colorReduce(scr_Image, dst_Image);imshow("image", scr_Image);imshow("colorReduce", dst_Image);waitKey(0);return 0;}void colorReduce(const Mat &Image, Mat &result, int div){result.create(Image.rows, Image.cols, Image.type());int nr = Image.rows;int nc = Image.cols * Image.channels();for (int j = 0; j < nr; j++){const uchar* data_in = Image.ptr<uchar>(j);uchar* data_out = result.ptr<uchar>(j);for (int i = 0; i < nc; i++){data_out[i] = data_in[i]/div*div + div/2;}}}

(2) program_two

#include "cv.h"#include "highgui.h"#include "cxcore.h"#include <iostream>using namespace std;using namespace cv;void colorReduce(Mat &Image, int div = 64);int main(void){Mat scr_Image = imread("F:\\1\\yellow_lane\\0.png", 1);colorReduce(scr_Image);imshow("image", scr_Image);waitKey(0);return 0;}void colorReduce(Mat &Image, int div){int nr = Image.rows;int nc = Image.cols;if (Image.isContinuous())// 连续图像,没有额外的填补像素{nc = nc * nr;nr = 1;}for (int j = 0; j < nr; j++){uchar* data = Image.ptr<uchar>(j);for (int i = 0; i < nc; i++){data[i] = data[i]/div*div + div/2;}}}

(3) program_three

#include "cv.h"#include "highgui.h"#include "cxcore.h"#include <iostream>using namespace std;using namespace cv;void colorReduce(Mat &Image, int div = 64);int main(void){Mat scr_Image = imread("F:\\1\\yellow_lane\\0.png", 1);colorReduce(scr_Image);imshow("image", scr_Image);waitKey(0);return 0;}void colorReduce(Mat &Image, int div){if (Image.isContinuous())// 连续图像,没有额外的填补像素{Image.reshape(1, Image.cols * Image.rows);}int nr = Image.rows;int nc = Image.cols;for (int j = 0; j < nr; j++){uchar* data = Image.ptr<uchar>(j);for (int i = 0; i < nc; i++){data[i] = data[i]/div*div + div/2;}}}




0 0
原创粉丝点击