影像處理之長條圖均衡化

來源:互聯網
上載者:User

影像處理之長條圖均衡化

基本思想:

長條圖圖均衡化是影像處理中的常用映像增強手段,長條圖均衡化的主要優點是

可以降低映像雜訊,增益圖像的局部顯示。對於常見的RGB映像,長條圖均衡化

可以分別在三個色彩通道上處理,基本的長條圖均衡化的公式為:

其中nj表示灰階級為Rk的像素的個數,L為映像中灰階總數,對於RGB來說L的

取值範圍為[0~255]總灰階級為256個。而R表示輸入映像的長條圖資料。根據輸

出的灰階值Sk計算出輸出像素的每個像素值,完成長條圖均衡化之後的像素處理

程式效果:


原始碼:

package com.gloomyfish.filter.study;import java.awt.image.BufferedImage;public class HistogramEFilter extends AbstractBufferedImageOp{@Overridepublic BufferedImage filter(BufferedImage src, BufferedImage dest) {int width = src.getWidth();        int height = src.getHeight();        if ( dest == null )            dest = createCompatibleDestImage( src, null );        int[] inPixels = new int[width*height];        int[] outPixels = new int[width*height];        getRGB( src, 0, 0, width, height, inPixels );        int[][] rgbhis = new int[3][256]; // RGB        int[][] newrgbhis = new int[3][256]; // after HE        for(int i=0; i<3; i++) {        for(int j=0; j<256; j++) {        rgbhis[i][j] = 0;        newrgbhis[i][j] = 0;        }        }        int index = 0;        int totalPixelNumber = height * width;        for(int row=0; row<height; row++) {        int ta = 0, tr = 0, tg = 0, tb = 0;        for(int col=0; col<width; col++) {        index = row * width + col;        ta = (inPixels[index] >> 24) & 0xff;                tr = (inPixels[index] >> 16) & 0xff;                tg = (inPixels[index] >> 8) & 0xff;                tb = inPixels[index] & 0xff;                // generate original source image RGB histogram                rgbhis[0][tr]++;                rgbhis[1][tg]++;                rgbhis[2][tb]++;        }        }                // generate original source image RGB histogram        generateHEData(newrgbhis, rgbhis, totalPixelNumber, 256);        for(int row=0; row<height; row++) {        int ta = 0, tr = 0, tg = 0, tb = 0;        for(int col=0; col<width; col++) {        index = row * width + col;        ta = (inPixels[index] >> 24) & 0xff;                tr = (inPixels[index] >> 16) & 0xff;                tg = (inPixels[index] >> 8) & 0xff;                tb = inPixels[index] & 0xff;                // get output pixel now...                tr = newrgbhis[0][tr];                tg = newrgbhis[1][tg];                tb = newrgbhis[2][tb];                                outPixels[index] = (ta << 24) | (tr << 16) | (tg << 8) | tb;        }        }        setRGB( dest, 0, 0, width, height, outPixels );        return dest;}/** *  * @param newrgbhis * @param rgbhis * @param totalPixelNumber * @param grayLevel [0 ~ 255] */private void generateHEData(int[][] newrgbhis, int[][] rgbhis, int totalPixelNumber, int grayLevel) {for(int i=0; i<grayLevel; i++) {newrgbhis[0][i] = getNewintensityRate(rgbhis[0], totalPixelNumber, i);newrgbhis[1][i] = getNewintensityRate(rgbhis[1], totalPixelNumber, i);newrgbhis[2][i] = getNewintensityRate(rgbhis[2], totalPixelNumber, i);}}private int getNewintensityRate(int[] grayHis, double totalPixelNumber, int index) {double sum = 0;for(int i=0; i<=index; i++) {sum += ((double)grayHis[i])/totalPixelNumber;}return (int)(sum * 255.0);}}
轉載請務必註明



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.