## 图像像素的线性叠加（第四天）

首先说一下API函数：Mat.convertTo()函数

假如我的原图读取时CV_8UC3，但是我想对图像的像素进行操作，uchar类型的精度不准确，所以就比比转换类型了,转换成CV_32FC3。

input_image1.convertTo(input_image1, CV_32FC3);

input_image1.convertTo(input_image1, CV_32FC3, 1 / 255.0);//所以需要转化成0-255区显示

 1 /** @brief Converts an array to another data type with optional scaling.
2
3     The method converts source pixel values to the target data type. saturate_cast\<\> is applied at
4     the end to avoid possible overflows:
5
6     \f[m(x,y) = saturate \_ cast<rType>( \alpha (*this)(x,y) +  \beta )\f]
7     @param m output matrix; if it does not have a proper size or type before the operation, it is
8     reallocated.
9     @param rtype desired output matrix type or, rather, the depth since the number of channels are the
10     same as the input has; if rtype is negative, the output matrix will have the same type as the input.
11     @param alpha optional scale factor.
12     @param beta optional delta added to the scaled values.
13      */
1  If the image is 8-bit unsigned, it is displayed as is.
2 -   If the image is 16-bit unsigned or 32-bit integer, the pixels are divided by 256. That is, the
3     value range [0,255\*256] is mapped to [0,255].
4 -   If the image is 32-bit floating-point, the pixel values are multiplied by 255. That is, the
5     value range [0,1] is mapped to [0,255].

手写API程序：用的是迭代器，当然可以用其它的方法：

 1 #include<iostream>
2 #include <opencv2/opencv.hpp>
3 #include <math.h>
4 using namespace cv;
5 using namespace std;
6
7 int main(int argc,char**argv)
8 {
11     input_image1.convertTo(input_image1, CV_32FC3, 1 / 255.0);
12     input_image2.convertTo(input_image2, CV_32FC3, 1 / 255.0);
13     if (input_image1.data==NULL||input_image2.data==NULL) {
14         return -1; cout << "can't open image.../";
15     }
16     imshow("Sourse image1", input_image1);
17     imshow("Source image2", input_image2);
18     CV_Assert(input_image1.rows == input_image2.rows&&input_image1.cols == input_image2.cols);
19     int Width = input_image1.rows;
20     int length = input_image2.rows;
21     Mat output_image;
22     output_image.create(input_image1.size(), input_image1.type());
23     Mat_<Vec3f>::iterator it1 = input_image1.begin<Vec3f>();
24     Mat_<Vec3f>::iterator it2 = input_image2.begin<Vec3f>();
25     Mat_<Vec3f>::iterator out = output_image.begin<Vec3f>();
26     while (it1 != input_image1.end<Vec3b>())
27     {
28         //---只是初学测试函数，当然可以封装成API一样的函数----//
29         (*out)[0] = 0.5*((*it1)[0]) + 0.5*((*it2)[0]) + 0;
30         (*out)[1] = 0.5*((*it1)[1]) + 0.5*((*it2)[1]) + 0;
31         (*out)[2] = 0.5*((*it1)[2]) + 0.5*((*it2)[2]) + 0;
32         it1++;
33         it2++;
34         out++;
35     }
37     imshow("Destinate image",output_image);
38     waitKey(0);
39     return 0;
40 }

opencv自带API函数：

 1 int main(int argc,char**argv)
2 {
5     //input_image1.convertTo(input_image1, CV_32FC3, 1 / 255.0);
6     //input_image2.convertTo(input_image2, CV_32FC3, 1 / 255.0);
7     if (input_image1.data==NULL||input_image2.data==NULL) {
8         return -1; cout << "can't open image.../";
9     }
10     imshow("Sourse image1", input_image1);
11     imshow("Source image2", input_image2);
12     CV_Assert(input_image1.rows == input_image2.rows&&input_image1.cols == input_image2.cols);
13     Mat output_image;
16     imshow("Destinate image",output_image);
17     waitKey(0);
18     return 0;
19 }