首页 > 其他 > 详细

opencv2使用查找表修改图像外观

时间:2014-04-22 18:13:07      阅读:638      评论:0      收藏:0      [点我收藏+]
#ifndef HISTOGRAM_H_
#define HISTOGRAM_H_
#include<opencv2/core/core.hpp>
#include<opencv2/highgui/highgui.hpp>
#include<opencv2/imgproc/imgproc.hpp>
#include <opencv2/calib3d/calib3d.hpp>
#include<iostream>
#include <vector>

 using namespace std;
 using namespace cv;
  
 class Histogram1D
 {
 private:
	 int histSize[1];
	 float hranges[2];
	 const float *ranges[1];
	 int channels[1];
 public:
	 Histogram1D();
	 cv::MatND getHistogram(const cv::Mat &image);
	 cv::Mat getHistogramImage(const cv::Mat &image);
	 cv::Mat applyLoopUp(const cv::Mat &image,const cv::Mat &lookup);
 };

#endif /* HISTOGRAM_H_ */

#include"Histogram1D.h"
Histogram1D::Histogram1D()
	 {
		 histSize[0]=256;
		 hranges[0]=0.0;
		 hranges[1]=255.0;
		 ranges[0]=hranges;
		 channels[0]=0;
	 };
cv::MatND Histogram1D::getHistogram(const cv::Mat &image)
	 {
		 cv::MatND hist;
		 cv::calcHist(&image,1,channels,cv::Mat(),hist,1,histSize,ranges);
		 return hist;
	 };
cv::Mat Histogram1D::getHistogramImage(const cv::Mat &image)
	 {
		 cv::MatND hist=getHistogram(image);
		 double maxVal=0;
		 double minVal=0;
		 cv::minMaxLoc(hist,&minVal,&maxVal,0,0);
		 cv::Mat histImg(histSize[0],histSize[0],CV_8U,cv::Scalar(255));
		 int hpt=static_cast<int>(0.9*histSize[0]);
		 for(int h=0;h<histSize[0];h++)
		 {
			 float binVal=hist.at<float>(h);
			 int intensity=static_cast<int>(binVal*hpt/maxVal);
			 cv::line(histImg,cv::Point(h,histSize[0]),cv::Point(h,histSize[0]-intensity),cv::Scalar::all(0));
		 }
		 return histImg;
	 };
cv::Mat Histogram1D::applyLoopUp(const cv::Mat &image,const cv::Mat &lookup)
{
	cv::Mat result;
	cv::LUT(image,lookup,result);
	return result;
}

#include"Histogram1D.h"
 
 int main()
 {
	 cv::Mat image=cv::imread("d:\\test\\opencv\\group.jpg",0);
	 if( !image.data ) exit(0);
	 Histogram1D h;
	 cv::namedWindow("Histogram");
	 cv::imshow("Histogram",h.getHistogramImage(image));
	 cv::Mat thresholded;
	 cv::threshold(image,thresholded,60,255,cv::THRESH_BINARY);
	 cv::namedWindow("Binary Image");
	 cv::imshow("Binary Image",thresholded);

	 int dim(256);
	 cv::Mat lut(1,&dim,CV_8U);
	 for(int i=0;i<256;i++)
	 {
		 lut.at<uchar>(i)=255-i;
	 }
	 cv::namedWindow("Negative image");
	 cv::imshow("Negative image",h.applyLoopUp(image,lut));
	 waitKey(0);
	 return 0;
 }


bubuko.com,布布扣bubuko.com,布布扣

 

opencv2使用查找表修改图像外观,布布扣,bubuko.com

opencv2使用查找表修改图像外观

原文:http://blog.csdn.net/williamfan21c/article/details/24309345

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!