网站建设参考文献资料,wordpress分类目录多级菜单,工业设计灵感网站,北京手机网站开发公司两者都是只对单通道使用#xff0c;对多通道的话 就需要分离通道处理再合并通道 两种方法#xff0c;第一个要运算次数太多了#xff0c;第二个只需要查表
伽马矫正函数#xff0c;这里用第二种方法#xff0c;且写法有点高级
int gammaCorrection(cv::Mat srcMat, cv::…两者都是只对单通道使用对多通道的话 就需要分离通道处理再合并通道 两种方法第一个要运算次数太多了第二个只需要查表
伽马矫正函数这里用第二种方法且写法有点高级
int gammaCorrection(cv::Mat srcMat, cv::Mat dstMat, float gamma) {//建立查询表unsigned char lut[256];for (int i 0; i 256; i){//saturate_cast防止像素值溢出如果值0,则返回0如果大于255则返回255lut[i] saturate_castuchar(pow((float)(i / 255.0f), gamma) * 255.0f);}srcMat.copyTo(dstMat);MatIterator_uchar it, end;for (it dstMat.beginuchar(), end dstMat.enduchar(); it ! end; it) {*it lut[(*it)];}return 0;}
就是建立了查找表然后计算查找表再遍历像素直接赋值查找表就不用计算了。 int readType 0;Mat srcMat imread(kjy.jpg);resize(srcMat, srcMat,Size(srcMat.rows*0.5, srcMat.rows * 0.5));cv::Mat dstMat;float gamma GAMMA_FACTOR;if (srcMat.type() CV_8UC1){gammaCorrection(srcMat, dstMat, gamma);}else {Mat channel[3];Mat out[3];float hist[3][256];//通道分离split(srcMat, channel);for (int i 0; i 3; i) {gammaCorrection(channel[i], out[i], gamma);}merge(out, 3, dstMat);}imshow(src, srcMat);imshow(dst, dstMat);waitKey(0);destroyAllWindows();
这就是grammar矫正的代码 直方图均衡化只对单通道有效果多通道的话先分离通道再合并一样的 equalizeHist(srcMat, equalizeHistMat); 计算直方图函数 int calcIntenHist(const cv::Mat src, float* dstHist)
{//输入必为单通道图if (src.type() ! CV_8UC1) {return -1;}memset(dstHist, 0, sizeof(float) * 256);int height src.rows;int width src.cols;//指针遍历for (int k 0; k height; k){// 获取第k行的首地址const uchar* inData src.ptruchar(k);//处理每个像素for (int i 0; i width; i){int gray inData[i];dstHist[gray];}}//直方图归一化float norm height * width;for (int n 0; n 256; n) {dstHist[n] dstHist[n] / norm;}return 0;
} 还进行了归一化
直方图画画函数 int drawIntenHist(cv::Mat histMat, float* srcHist, int bin_width, int bin_heght)
{histMat.create(bin_heght, 256 * bin_width, CV_8UC3);histMat Scalar(255, 255, 255);float maxVal *std::max_element(srcHist, srcHist 256);for (int i 0; i 256; i) {Rect binRect;binRect.x i * bin_width;float height_i (float)bin_heght * srcHist[i] / maxVal;binRect.height (int)height_i;binRect.y bin_heght - binRect.height;binRect.width bin_width;rectangle(histMat, binRect, CV_RGB(255, 0, 0), -1);}return 0;
} float height_i (float)bin_heght * srcHist[i] / maxVal;是防止不够高度大小 要进行的高度归一 直方图均衡化的完整代码 float srcHist[256];float dstHist[256];Mat dstHistMat;Mat srcHistMat;Mat histMat[3];Mat equalizeHistMat;cv::Mat dstMat1;int bin_width 2;int bin_heigth 100;if (srcMat.type() CV_8UC1) {equalizeHist(srcMat, equalizeHistMat);imshow(src, srcMat);imshow(equalizeHistMat, equalizeHistMat);waitKey(0);destroyAllWindows();calcIntenHist(dstMat1, dstHist);drawIntenHist(dstHistMat, dstHist, 3, 100);imshow(dstMat hist, dstHistMat);calcIntenHist(srcMat, srcHist);drawIntenHist(srcHistMat, srcHist, 3, 100);imshow(srcMat hist, srcHistMat);waitKey(0);destroyAllWindows();}else{Mat channel[3];Mat out[3];float hist[3][256];split(srcMat, channel);for (int i 0; i 3; i) {equalizeHist(channel[i], out[i]);calcIntenHist(out[i], hist[i]);drawIntenHist(histMat[i], hist[i], bin_width, bin_heigth);//按照channel编号命名窗口stringstream ss;ss i;string histWindow Hist of chanel ss.str();string matWindow Image of chanel ss.str();imshow(histWindow, histMat[i]);imshow(matWindow, out[i]);}merge(out, 3, dstMat1);cv::Mat grayMat;cv::Mat graydstMat;cvtColor(srcMat, grayMat, CV_BGR2GRAY);cvtColor(dstMat1, graydstMat, CV_BGR2GRAY);//计算并绘制直方图calcIntenHist(graydstMat, dstHist);drawIntenHist(dstHistMat, dstHist, 3, 100);imshow(dstMat, dstMat1);imshow(dstMat hist, dstHistMat);calcIntenHist(grayMat, srcHist);drawIntenHist(srcHistMat, srcHist, 3, 100);imshow(srcMat hist, srcHistMat);imshow(srcMat, srcMat);waitKey(0);destroyAllWindows();}return 0;}