opencv3/C++图像像素操作的示例分析-创新互联
这篇文章主要介绍了opencv3/C++图像像素操作的示例分析,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。
创新互联专业网站设计、成都做网站,集网站策划、网站设计、网站制作于一体,网站seo、网站优化、网站营销、软文推广等专业人才根据搜索规律编程设计,让网站在运行后,在搜索中有好的表现,专业设计制作为您带来效益的网站!让网站建设为您创造效益。RGB图像转灰度图
RGB图像转换为灰度图时通常使用:
进行转换,以下尝试通过其他对图像像素操作的方式将RGB图像转换为灰度图像。
#include#include using namespace cv; int main() { //像素操作 Mat src,dst; src = imread("E:/image/image/daibola.jpg"); if(src.empty()) { printf("can not load image \n"); return -1; } namedWindow("input"); imshow("input",src); dst.create(src.size(), src.type()); for(int row = 0; row < src.rows; row++) { for(int col = 0; col < src.cols; col++) { int b = src.at (row, col)[0]; int g = src.at (row, col)[1]; int r = src.at (row, col)[2]; dst.at (row, col)[0] = max(r,max(g,b)); dst.at (row, col)[1] = max(r,max(g,b)); dst.at (row, col)[2] = max(r,max(g,b)); } } namedWindow("output"); imshow("output",dst); waitKey(); }
同理使用min(r,min(g,b))可以看到由于选择了较小的灰度值图像会明显变暗:
图像线性增强
通过对图像像素操作(线性变换),实现图像的线性增强。
#include#include using namespace cv; int main() { Mat src1, dst; src1 = imread("E:/image/image/im1.jpg"); if(src1.empty()) { printf("can not load im1 \n"); return -1; } double alpha = 1.2, beta = 50; dst = Mat::zeros(src1.size(), src1.type()); for(int row = 0; row < src1.rows; row++) { for(int col = 0; col < src1.cols; col++) { if(src1.channels() == 3) { int b = src1.at (row, col)[0]; int g = src1.at (row, col)[1]; int r = src1.at (row, col)[2]; dst.at (row, col)[0] = saturate_cast (b*alpha + beta); dst.at (row, col)[1] = saturate_cast (g*alpha + beta); dst.at (row, col)[2] = saturate_cast (r*alpha + beta); } else if (src1.channels() == 1) { float v = src1.at (row, col); dst.at (row, col) = saturate_cast (v*alpha + beta); } } } namedWindow("output",CV_WINDOW_AUTOSIZE); imshow("output", dst); waitKey(); return 0; }
掩膜操作调整图像对比度
使用一个3×3掩模增强图像对比度:
#include#include using namespace cv; int main() { Mat src, dst; src = imread("E:/image/image/daibola.jpg"); CV_Assert(src.depth() == CV_8U); if(!src.data) { printf("can not load image \n"); return -1; } src.copyTo(dst); for(int row = 1; row<(src.rows - 1); row++) { const uchar* previous = src.ptr (row - 1); const uchar* current = src.ptr (row); const uchar* next = src.ptr (row + 1); uchar* output = dst.ptr (row); for(int col = src.channels(); col < (src.cols - 1)*src.channels(); col++) { *output = saturate_cast (9 * current[col] - 2*previous[col] - 2*next[col] - 2*current[col - src.channels()] - 2*current[col + src.channels()]); output++; } } namedWindow("image", CV_WINDOW_AUTOSIZE); imshow("image",dst); waitKey(); return 0; }
像素重映射
利用cv::remap实现像素重映射;
cv::remap参数说明:
Remap( InputArray src,// 输入图像 OutputArray dst,// 输出图像 InputArray map1,// 映射表1(CV_32FC1/CV_32FC2) InputArray map2,// 映射表2(CV_32FC1/CV_32FC2) int interpolation,// 选择的插值 int borderMode,// 边界类型(BORDER_CONSTANT) const Scalar borderValue// 颜色 )
插值方法:
CV_INTER_NN =0, CV_INTER_LINEAR =1, CV_INTER_CUBIC =2, CV_INTER_AREA =3, CV_INTER_LANCZOS4 =4
通过像素重映射实现图像垂直翻转:
#includeusing namespace cv; int main() { Mat src,dst; src = imread("E:/image/image/daibola.jpg"); if(src.empty()) { printf("can not load image \n"); return -1; } namedWindow("input", CV_WINDOW_AUTOSIZE); imshow("input", src); Mat mapx,mapy; mapx.create(src.size(), CV_32FC1); mapy.create(src.size(), CV_32FC1); for(int row = 0; row < src.rows; row++) { for(int col = 0; col < src.cols; col++) { mapx.at (row, col) = col; mapy.at (row, col) = src.rows - row - 1; } } remap(src, dst, mapx, mapy, CV_INTER_NN, BORDER_CONSTANT, Scalar(0,255,255)); namedWindow("output", CV_WINDOW_AUTOSIZE); imshow("output",dst); waitKey(); return 0; }
感谢你能够认真阅读完这篇文章,希望小编分享的“opencv3/C++图像像素操作的示例分析”这篇文章对大家有帮助,同时也希望大家多多支持创新互联,关注创新互联行业资讯频道,更多相关知识等着你来学习!
本文题目:opencv3/C++图像像素操作的示例分析-创新互联
当前路径:http://scyanting.com/article/djdsgi.html