直方图均衡化CImg实现

这篇博客是关于试用CImg库来实现灰度图和彩色图的直方图均衡化操作。感觉效果还不错,除了彩色图在均衡化时会有一定的色彩失真。

C++代码实现:

//
//  hEqualization.hpp
//  直方图均衡化
//
//  Created by Alala on 2017/3/20.
//  Copyright © 2017年 Alala. All rights reserved.
//

#ifndef hEqualization_h
#define hEqualization_h
#include<CImg.h>

using namespace cimg_library;


class hEqualization {
private:
    CImg<unsigned char> image;
    int x;
    int y;
    float totalPixel;
    
public:
    hEqualization();
    hEqualization(CImg<unsigned char> img);
    void setImage(CImg<unsigned char> img);
    CImg<unsigned char> toGray();
    void grayProcess();
    void RGBProcess();
};

hEqualization::hEqualization() {
    x = 0;
    y = 0;
    totalPixel = 0;
}
hEqualization::hEqualization(CImg<unsigned char> img) {
    image = img;
    x = image.width();
    y = image.height();
    totalPixel = x * y;
}
void hEqualization::setImage(CImg<unsigned char> img) {
    image = img;
    x = image.width();
    y = image.height();
    totalPixel = x * y;
}

CImg<unsigned char> hEqualization::toGray() {
    CImg<unsigned char> gray = image;
    cimg_forXY(image, x, y) {
        int r = image(x, y, 0);
        int g = image(x, y, 1);
        int b = image(x, y, 2);
        double temp = (r * 0.2126 + g * 0.7152 + b * 0.0722);
        gray(x, y, 0) = temp;
        gray(x, y, 1) = temp;
        gray(x, y, 2) = temp;
    }
    return gray;
}
void hEqualization::grayProcess() {
    CImg<unsigned char> gray = toGray();
    gray.display("gray");
    float pixels[256];
    float probability[256];
    float newP[256];
    memset(&pixels, 0, 256*sizeof(float));
    cimg_forXY(image, x, y) {
        pixels[int(image(x, y, 0))]++;
    }
    for(int i = 0; i < 256; i++) {
        probability[i] = pixels[i] / totalPixel;
        if(i == 0) {
            newP[i] = probability[i];
        } else {
            newP[i] = probability[i] + newP[i-1];
        } 
    }
    cimg_forXY(image, x, y) {
        int temp = gray(x, y, 0);
        temp = int(newP[temp] * 255 + 0.5);
        gray(x, y, 0) = temp;
        gray(x, y, 1) = temp;
        gray(x, y, 2) = temp;
    }
    gray.display("test");   
}
void hEqualization::RGBProcess() {
    float pixelsR[256];
    float pixelsG[256];
    float pixelsB[256];
    memset(&pixelsR, 0, 256*sizeof(float));
    memset(&pixelsG, 0, 256*sizeof(float));
    memset(&pixelsB, 0, 256*sizeof(float));
    
    float probabilityR[256];
    float probabilityG[256];
    float probabilityB[256];
    
    float newPR[256];
    float newPG[256];
    float newPB[256];
    cimg_forXY(image, x, y) {
        pixelsR[int(image(x, y, 0))]++;
        pixelsG[int(image(x, y, 1))]++;
        pixelsB[int(image(x, y, 2))]++;
    }
    for(int i = 0; i < 256; i++) {
        probabilityR[i] = pixelsR[i] / totalPixel;
        probabilityG[i] = pixelsG[i] / totalPixel;
        probabilityB[i] = pixelsB[i] / totalPixel;
        if(i == 0) {
            newPR[i] = probabilityR[i];
            newPG[i] = probabilityG[i];
            newPB[i] = probabilityB[i];
        } else {
            newPR[i] = probabilityR[i] + newPR[i-1];
            newPG[i] = probabilityG[i] + newPG[i-1];
            newPB[i] = probabilityB[i] + newPB[i-1];
        }
    }
    CImg<unsigned char> color = image;
    cimg_forXY(image, x, y) {
        int tempR = image(x, y, 0);
        int tempG = image(x, y, 1);
        int tempB = image(x, y, 2);
        tempR = int(newPR[tempR] * 255 + 0.5);
        tempG = int(newPG[tempG] * 255 + 0.5);
        tempB = int(newPB[tempB] * 255 + 0.5);
        color(x, y, 0) = tempR;
        color(x, y, 1) = tempG;
        color(x, y, 2) = tempB;
    }
    color.display("color");   
}



#endif /* hEqualization_h */

 

转载于:https://www.cnblogs.com/alala713/p/6618420.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值