对图像进行指数变换并分别以三通道输出

2018/8/23正确版本如下:

#include <iostream>  
#include <string.h>
#include <opencv2/core/core.hpp>  
#include <opencv2/highgui/highgui.hpp>  
#include <opencv2\core\mat.hpp>
#include<opencv2/opencv.hpp>
#define e 2.71828182845
using namespace cv;
using namespace std;

void main()
{
	Mat img = imread("pic2.jpg");//读取图像img。0表示转换为灰度图像读入
	imshow("原图", img);
	waitKey(0);
	int height = img.rows;
	int width = img.cols; 
	//浮点变换
	Mat fimg(height, width, CV_32FC3);
	img.convertTo(fimg, CV_32FC3,1/255.0);
	
	//将修改后的像素值放入另一个图片中
	Mat fimg2(height,width, CV_32FC3);
	for (int row = 0; row < height; row++)
		for (int col = 0; col < width; col++)
		{
			
			fimg2.at<Vec3f>(row, col)[0] = float(pow(fimg.at<Vec3f>(row, col)[0], e));
			fimg2.at<Vec3f>(row, col)[1] = float(pow(fimg.at<Vec3f>(row, col)[1], e));
			fimg2.at<Vec3f>(row, col)[2] = float(pow(fimg.at<Vec3f>(row, col)[2], e));
		}
	namedWindow("rgb", 0);
	imshow("rgb",fimg2);
	waitKey(0);
	//三通道进行输出
	for (int i = 0; i < 3; i++)

	{

		Mat bgr(height, width, CV_32FC3, Scalar(0, 0, 0));

		Mat out[] = { bgr };

		int from_to[] = { i, i };

		mixChannels(&fimg2, 1, out, 1, from_to, 1);

		//获得其中一个通道的数据进行分析
		imshow("1 channel", bgr);
		waitKey();

	}
}

错误版本:

#include <iostream>  
#include <string.h>
#include <opencv2/core/core.hpp>  
#include <opencv2/highgui/highgui.hpp>  
#include <opencv2\core\mat.hpp>
#include<opencv2/opencv.hpp>
#define PI 3.1415926
using namespace cv;
using namespace std;

void main()
{
    Mat img = imread("pic1.jpg");//读取图像img。0表示转换为灰度图像读入

    int height = img.rows;
    int width = img.cols;
    //浮点变换
    Mat fimg(img.rows, img.cols, CV_64FC1);
    img.convertTo(fimg, CV_64FC1);
    double max = img.at<double>(0, 0);
    for (int row = 0; row < height; row++)
        for (int col = 0; col < width; col++)
        {
            if (max < fimg.at<double>(row, col))
                max = fimg.at<double>(row, col);
            /*        cout << "max:" << max << "  ";*/
        }

    for (int row = 0; row < height; row++)
        for (int col = 0; col < width; col++)
        {
            cout << "img.data" << double(fimg.at<double>(row, col)/max) << "  ";
            fimg.at<double>(row, col) = exp(fimg.at<double>(row, col) / max);
            cout << "exp(img.data)" << double(fimg.at<double>(row, col)) << "\n";
        }
    for (int i = 0; i < 3; i++)

    {

        Mat bgr(height, width, CV_8UC3, Scalar(0, 0, 0));

        Mat out[] = { bgr };

        int from_to[] = { i, i };

        mixChannels(&fimg, 1, out, 1, from_to, 1);

        //获得其中一个通道的数据进行分析
        imshow("1 channel", bgr);
        waitKey();

    }
}

 

©️2020 CSDN 皮肤主题: 书香水墨 设计师:CSDN官方博客 返回首页