[OpenCV] Samples 09: image
根据需求,转化为不同的颜色格式,split后处理各自通道。
plImage <==> Mat 格式转换
Mat --> plImage 简单写法:
IplImage copy = mat_img;
IplImage* new_image = copy;
cvWriteFrame( wrVideo1, new_image );
#include <stdio.h>
#include <iostream>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/core/utility.hpp> using namespace cv; // all the new API is put into "cv" namespace. Export its content
using namespace std; static void help()
{
cout <<
"\nThis program shows how to use cv::Mat and IplImages converting back and forth.\n"
"It shows reading of images, converting to planes and merging back, color conversion\n"
"and also iterating through pixels.\n"
"Call:\n"
"./image [image-name Default: ../data/lena.jpg]\n" << endl;
} // enable/disable use of mixed API in the code below.
#define DEMO_MIXED_API_USE 1 #ifdef DEMO_MIXED_API_USE
# include <opencv2/highgui/highgui_c.h>
# include <opencv2/imgcodecs/imgcodecs_c.h>
#endif int main( int argc, char** argv )
{
cv::CommandLineParser parser(argc, argv, "{help h | |}{@image|../data/lena.jpg|}");
if (parser.has("help"))
{
help();
return 0;
}
string imagename = parser.get<string>("@image"); /*
* Jeff: How to transform between them.
* plImage <==> Mat
*/ #if DEMO_MIXED_API_USE
//! [iplimage]
Ptr<IplImage> iplimg(cvLoadImage(imagename.c_str())); // Ptr<T> is safe ref-counting pointer class
if(!iplimg)
{
fprintf(stderr, "Can not load image %s\n", imagename.c_str());
return -1;
}
Mat img = cv::cvarrToMat(iplimg); // cv::Mat replaces the CvMat and IplImage, but it's easy to convert
// between the old and the new data structures (by default, only the header
// is converted, while the data is shared)
//! [iplimage]
#else
Mat img = imread(imagename); // the newer cvLoadImage alternative, MATLAB-style function
if(img.empty())
{
fprintf(stderr, "Can not load image %s\n", imagename.c_str());
return -1;
}
#endif if( img.empty() ) // check if the image has been loaded properly
return -1; Mat img_yuv;
cvtColor(img, img_yuv, COLOR_BGR2YCrCb); // convert image to YUV color space. The output image will be created automatically vector<Mat> planes; // Vector is template vector class, similar to STL's vector. It can store matrices too.
split(img_yuv, planes); // split the image into separate color planes #if 1
/*
* Jeff: MatIterator_< >
* Mat 单元处理
*/ // method 1. process Y plane using an iterator
MatIterator_<uchar> it = planes[0].begin<uchar>(), it_end = planes[0].end<uchar>();
for(; it != it_end; ++it)
{
double v = *it*1.7 + rand()%21-10;
// 考虑:为什么上面的函数会用到saturate_cast呢?
// 因为无论是加是减,乘除,都会超出一个像素灰度值的范围(0~255)
// 所以,所以当运算完之后,结果为负,则转为0,结果超出255,则为255。
*it = saturate_cast<uchar>(v*v/255.);
} // method 2. process the first chroma plane using pre-stored row pointer.
// method 3. process the second chroma plane using individual element access
for( int y = 0; y < img_yuv.rows; y++ )
{
uchar* Uptr = planes[1].ptr<uchar>(y);
for( int x = 0; x < img_yuv.cols; x++ )
{
Uptr[x] = saturate_cast<uchar>((Uptr[x]-128)/2 + 128);
uchar& Vxy = planes[2].at<uchar>(y, x);
Vxy = saturate_cast<uchar>((Vxy-128)/2 + 128);
}
} #else
Mat noise(img.size(), CV_8U); // another Mat constructor; allocates a matrix of the specified size and type
randn(noise, Scalar::all(128), Scalar::all(20)); // fills the matrix with normally distributed random values;
// there is also randu() for uniformly distributed random number generation
GaussianBlur(noise, noise, Size(3, 3), 0.5, 0.5); // blur the noise a bit, kernel size is 3x3 and both sigma's are set to 0.5 const double brightness_gain = 0;
const double contrast_gain = 1.7;
#if DEMO_MIXED_API_USE
// it's easy to pass the new matrices to the functions that only work with IplImage or CvMat:
// step 1) - convert the headers, data will not be copied
IplImage cv_planes_0 = planes[0], cv_noise = noise;
// step 2) call the function; do not forget unary "&" to form pointers
cvAddWeighted(&cv_planes_0, contrast_gain, &cv_noise, 1, -128 + brightness_gain, &cv_planes_0);
#else
addWeighted(planes[0], contrast_gain, noise, 1, -128 + brightness_gain, planes[0]);
#endif
const double color_scale = 0.5;
// Mat::convertTo() replaces cvConvertScale. One must explicitly specify the output matrix type (we keep it intact - planes[1].type())
planes[1].convertTo(planes[1], planes[1].type(), color_scale, 128*(1-color_scale));
// alternative form of cv::convertScale if we know the datatype at compile time ("uchar" here).
// This expression will not create any temporary arrays and should be almost as fast as the above variant
planes[2] = Mat_<uchar>(planes[2]*color_scale + 128*(1-color_scale)); // Mat::mul replaces cvMul(). Again, no temporary arrays are created in case of simple expressions.
planes[0] = planes[0].mul(planes[0], 1./255);
#endif /*
* Jeff --> split, merge
*/
// now merge the results back
merge(planes, img_yuv);
// and produce the output RGB image
cvtColor(img_yuv, img, COLOR_YCrCb2BGR); // this is counterpart for cvNamedWindow
namedWindow("image with grain", WINDOW_AUTOSIZE);
#if DEMO_MIXED_API_USE
// this is to demonstrate that img and iplimg really share the data - the result of the above
// processing is stored in img and thus in iplimg too.
cvShowImage("image with grain", iplimg);
#else
imshow("image with grain", img);
#endif
waitKey(); return 0;
// all the memory will automatically be released by Vector<>, Mat and Ptr<> destructors.
}
[OpenCV] Samples 09: image的更多相关文章
- [OpenCV] Samples 09: plImage <==> Mat
根据需求,转化为不同的颜色格式,split后处理各自通道. plImage <==> Mat 格式转换 Mat --> plImage 简单写法: IplImage copy = m ...
- [OpenCV] Samples 10: imagelist_creator
yaml写法的简单例子.将 $ ./ 1 2 3 4 5 命令的参数(代表图片地址)写入yaml中. 写yaml文件. 参考:[OpenCV] Samples 06: [ML] logistic re ...
- [OpenCV] Samples 16: Decompose and Analyse RGB channels
物体的颜色特征决定了灰度处理不是万能,对RGB分别处理具有相当的意义. #include <iostream> #include <stdio.h> #include &quo ...
- [OpenCV] Samples 06: [ML] logistic regression
logistic regression,这个算法只能解决简单的线性二分类,在众多的机器学习分类算法中并不出众,但它能被改进为多分类,并换了另外一个名字softmax, 这可是深度学习中响当当的分类算法 ...
- [OpenCV] Samples 06: logistic regression
logistic regression,这个算法只能解决简单的线性二分类,在众多的机器学习分类算法中并不出众,但它能被改进为多分类,并换了另外一个名字softmax, 这可是深度学习中响当当的分类算法 ...
- [OpenCV] Samples 13: opencv_version
cv::CommandLineParser的使用. I suppose CommandLineParser::has("something") should be true whe ...
- [OpenCV] Samples 12: laplace
先模糊再laplace,也可以替换为sobel等. 变换效果后录成视频,挺好玩. #include "opencv2/videoio/videoio.hpp" #include & ...
- [OpenCV] Samples 05: convexhull
得到了复杂轮廓往往不适合特征的检测,这里再介绍一个点集凸包络的提取函数convexHull,输入参数就可以是contours组中的一个轮廓,返回外凸包络的点集 ---- 如此就能去掉凹进去的边. 对于 ...
- [OpenCV] Samples 03: cout_mat
操作Mat元素时:I.at<double>(1,1) = CV_PI; /* * * cvout_sample just demonstrates the serial out capab ...
随机推荐
- modal的使用
$modal是一个可以迅速创建模态窗口的服务,创建部分页,控制器,并关联他们 $modal仅有一个方法open(options) templateUrl:模态窗口的地址 template:用于显示ht ...
- WideCharToMultiByte和MultiByteToWideChar函数的用法
为了支持Unicode编码,需要多字节与宽字节之间的相互转换.这两个系统函数在使用时需要指定代码页,在实际应用过程中遇到乱码问题,然后重新阅读<Windows核心编程>,总结出正确的用法. ...
- eclipse左边导航package explorer自动定位
eclipse或myeclipse中右边编辑界面点击 左边导航自动定位 左导航Package Explorer的右上角有一个黄色双向箭头图标,鼠标移动到上面提示"Link with ...
- C++ std::map::erase用法及其陷阱
1.引入: STL的map中有一个erase方法用来从一个map中删除制定的节点 eg: map<string,string> mapTest; typedef map<string ...
- 在Python中调用glutInit遇到OpenGL.error.NullFunctionError的解决方法
在window10 64bit + Python环境下,通过pip install PyOpenGL成功之后,无奈执行到glutInit()时候总是报错. OpenGL.error.NullFunct ...
- Hadoop2.20集群搭建
hadoop2.0已经发布了稳定版本了,增加了很多特性,比如HDFS HA.YARN等. 注意:apache提供的hadoop-2.2.0的安装包是在32位操作系统编译的,因为hadoop依赖一些C+ ...
- 重量级Orchard模块发布 - 模块生成工具RaisingStudio.ModuleGenerator
可以从这里下载安装(http://gallery.orchardproject.net/List/Modules/Orchard.Module.RaisingStudio.ModuleGenerato ...
- 算法:x的n次方
该题是用来公司教学,并无难度.用于说明算法效率差异以及循环和递归的效率差别. package practice; import java.math.BigDecimal; /** * @author ...
- phonegap 基础原理
phonegap是web app 下调用移动OS原生API的库.在整个压缩包结构中主要分三块: 1.cordova.js,前端的js库:功能是提供js的API接口,最终调用的是promp方法,如下: ...
- 【译】什么导致了Context泄露:Handler&内部类
思考下面代码 public class SampleActivity extends Activity { private final Handler mLeakyHandler = new Hand ...