首页 > 代码库 > 遍历图像
遍历图像
一、高效遍历图片
#include<opencv2\opencv.hpp> #include<iostream> #include<string> using namespace cv; using namespace std; void colorReduce(Mat &image, int div = 64); int main() { Mat image = imread("C:\\Users\\Nelsoner\\Desktop\\Camera Roll\\05.jpg"); namedWindow("hah"); for (int i = 1; i < 180; i += 10) { colorReduce(image, i); waitKey(550); imshow("hah", image); if (i == 178) { break; } } } void colorReduce(Mat &image, int div) { int nl = image.rows; //行数 int nc = image.cols * image.channels(); //每行的元素个数 if (image.isContinuous()) //判断这幅图是否对行进行了填补,如果返回值是真,说明这图没有进行填补 { nc = nc*nl; nl = 1; } //对于连续图形,本循环只执行1次 for (int j = 0; j < nl; j++) { //得到第j行的首地址 uchar *data = http://www.mamicode.com/image.ptr(j); for (int i = 0; i < nc; i++) { //处理每个像素 //data[i] = data[i] / div*div + div / 2; //颜色缩减公式 data[i] = data[i] - data[i] % div + div / 2; } } } void colorReduce1(Mat &image, int div) { int nl = image.rows; int nc = image.cols; if (image.isContinuous()) { nc = nc * nl; nl = 1; } int n = static_cast<int>(log(static_cast<double>(div)) / log(2.0)); uchar mask = 0xFF << n; //位运算 for (int j = 0; j < nl; j++) { uchar *data = http://www.mamicode.com/image.ptr (j); for (int i = 0; i < nc; i++) { *data ++= *data&mask + div / 2; *data++ = *data&mask + div / 2; *data++ = *data&mask + div / 2; } } }
二、迭代器遍历图像
1 void colorReduce(Mat &image, int div = 64); 2 int main() { 3 Mat image = imread("C:\\Users\\Nelsoner\\Desktop\\Camera Roll\\05.jpg"); 4 namedWindow("hah"); 5 for (int i = 1; i < 180; i += 10) { 6 colorReduce(image, i); 7 waitKey(550); 8 imshow("hah", image); 9 if (i == 178) { 10 break; 11 } 12 13 } 14 } 15 void colorReduce(Mat &image, int div) { 16 //得到初始位置的迭代器 17 Mat_<Vec3b>::iterator it = image.begin<Vec3b>(); 18 //得到结束位置 19 Mat_<Vec3b>::iterator itEnd = image.end<Vec3b>(); 20 21 for (; it != itEnd; it++) { 22 (*it)[0] = (*it)[0]/div*div + div / 2; 23 (*it)[1] = (*it)[1] / div*div + div / 2; 24 (*it)[2] = (*it)[2] / div*div + div / 2; 25 26 } 27 }
三、遍历图像和邻域操作(图像锐化)
1 #include<opencv2\opencv.hpp> 2 #include<iostream> 3 #include<string> 4 5 using namespace cv; 6 using namespace std; 7 8 void sharpen(const Mat &image, Mat &result); 9 int main() { 10 Mat image = imread("C:\\Users\\Nelsoner\\Desktop\\Camera Roll\\05.jpg"); 11 Mat result; 12 sharpen(image, result); 13 namedWindow("hah"); 14 imshow("hah", result); 15 //namedWindow("heh"); 16 //imshow("heh", image); 17 waitKey(10000); 18 19 } 20 21 void sharpen(const Mat &image, Mat &result) { //锐化 22 result.create(image.size(), image.type()); 23 for (int j = 1; j < image.rows - 1; j++) { //处理除了第一行和最后一行之外的所有行 24 const uchar* previous = image.ptr<const uchar>(j - 1); //上一行 25 const uchar* current = image.ptr<const uchar>(j); //当前行 26 const uchar* next = image.ptr<const uchar>(j + 1); //下一行 27 uchar* output = result.ptr<uchar>(j); //输出行 28 for (int i = 1; i < image.cols - 1; i++) { 29 * output ++= saturate_cast<uchar>(10 * current[i] - current[i - 1] - current[i + 1] - previous[i] - next[i]); 30 } 31 } 32 result.row(0).setTo(Scalar(0)); 33 result.row(result.rows - 1).setTo(Scalar(0)); 34 result.col(0).setTo(Scalar(0)); 35 result.col(result.cols - 1).setTo(Scalar(0)); 36 }
1 void sharpen2D(const Mat &image, Mat &result) { 2 Mat kernel(3,3,CV_32F,Scalar(0)); //构造核 3 //对核进行赋值 4 kernel.at<float>(1, 1) = 5.0; 5 kernel.at<float>(0, 1) = -1.0; 6 kernel.at<float>(2, 1) = -1.0; 7 kernel.at<float>(1, 0) = -1.0; 8 kernel.at<float>(1, 2) = -1.0; 9 10 filter2D(image,result,image.depth(),kernel); 11 }
遍历图像
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。