局部增强

#include<iostream>
#include<cv.hpp>
#include<highgui.hpp>
using namespace std;
using namespace cv;
int pos_archor_width = 1;
int pos_archor_heigh = 1;
int mat_width;
int mat_heigh;
int kernel_width = 3;
int kernel_heigh = 3;
const Vec3b& getAvg(Mat in, int **kernel, int pos_heigh, int pos_width)
{
    int temp_width_kernel_start, temp_width_kernel_end;
    int temp_heigh_kernel_start, temp_heigh_kernel_end;
    if (pos_width >= pos_archor_width)
        temp_width_kernel_start = 0;
    else
        temp_width_kernel_start = pos_archor_width - pos_width;
    if (mat_width - 1 - pos_width >= kernel_width - 1 - pos_archor_width)
        temp_width_kernel_end = kernel_width - 1;
    else
        temp_width_kernel_end = mat_width - 1 - pos_width + pos_archor_width;
    if (pos_heigh >= pos_archor_heigh)
        temp_heigh_kernel_start = 0;
    else
        temp_heigh_kernel_start = pos_archor_heigh - pos_heigh;
    if (mat_heigh - 1 - pos_heigh >= kernel_heigh - 1 - pos_archor_heigh)
        temp_heigh_kernel_end = kernel_heigh - 1;
    else
        temp_heigh_kernel_end = mat_heigh - 1 - pos_heigh + pos_archor_heigh;

    double all1 = 0;
    double all2 = 0;
    double all3 = 0;
    int x = 0;
    Vec3d vec;
    //int vec;
    int size =((temp_heigh_kernel_end - temp_heigh_kernel_start + 1)*(temp_width_kernel_end - temp_width_kernel_start + 1));
    //cout<<in.depth();
    for (int i = temp_heigh_kernel_start; i <= temp_heigh_kernel_end; ++i)
    {
        int y = 0;

        for (int j = temp_width_kernel_start; j <= temp_width_kernel_end; ++j)
        {
            vec = in.at<Vec3b>((pos_heigh - (pos_archor_heigh - temp_heigh_kernel_start) + x), (pos_width - (pos_archor_width - temp_width_kernel_start) + y));
            all1 += (int)vec[0];
            all2 += (int)vec[1];
            all3 += (int)vec[2] ;
            //all3 += vec;

            y++;
        }
        x++;
    }
    Vec3d tmp;
    //int tmp;
    tmp[0] = all1/size;
    tmp[1] = all2/size;
    tmp[2] = all3/size;
    //tmp = all3 / size;
    return tmp;
    
}

const Vec3b& getVar(Mat in, Vec3d avg/*int avg*/, int pos_heigh, int pos_width)
{
    int temp_width_kernel_start, temp_width_kernel_end;
    int temp_heigh_kernel_start, temp_heigh_kernel_end;
    if (pos_width >= pos_archor_width)
        temp_width_kernel_start = 0;
    else
        temp_width_kernel_start = pos_archor_width - pos_width;
    if (mat_width - 1 - pos_width >= kernel_width - 1 - pos_archor_width)
        temp_width_kernel_end = kernel_width - 1;
    else
        temp_width_kernel_end = mat_width - 1 - pos_width + pos_archor_width;
    if (pos_heigh >= pos_archor_heigh)
        temp_heigh_kernel_start = 0;
    else
        temp_heigh_kernel_start = pos_archor_heigh - pos_heigh;
    if (mat_heigh - 1 - pos_heigh >= kernel_heigh - 1 - pos_archor_heigh)
        temp_heigh_kernel_end = kernel_heigh - 1;
    else
        temp_heigh_kernel_end = mat_heigh - 1 - pos_heigh + pos_archor_heigh;

    double all1 = 0;
    double all2 = 0;
    double all3 = 0;
    int x = 0;
    Vec3d vec;
    //int vec;
    vec = avg;
    int size = 0;
    size = ((temp_heigh_kernel_end - temp_heigh_kernel_start+1)*(temp_width_kernel_end - temp_width_kernel_start+1));
    //cout<<in.depth();
    for (int i = temp_heigh_kernel_start; i <= temp_heigh_kernel_end; ++i)
    {
        int y = 0;

        for (int j = temp_width_kernel_start; j <= temp_width_kernel_end; ++j)
        {
            //vec = in.at<Vec3b>((pos_heigh - (pos_archor_heigh - temp_heigh_kernel_start) + x), (pos_width - (pos_archor_width - temp_width_kernel_start) + y));
            vec = in.at<Vec3b>((pos_heigh - (pos_archor_heigh - temp_heigh_kernel_start) + x), (pos_width - (pos_archor_width - temp_width_kernel_start) + y));

            //cout << vec << endl;
        //    all1 += (in.at<Vec3b>(i,j)[0]- (int)vec[0])*(in.at<Vec3b>(i, j)[0] - (int)vec[0]) / ((temp_heigh_kernel_end - temp_heigh_kernel_start + 1)*(temp_width_kernel_end - temp_width_kernel_start + 1));
            //all2 += (in.at<Vec3b>(i, j)[1] - (int)vec[1])*(in.at<Vec3b>(i, j)[1] - (int)vec[1])/((temp_heigh_kernel_end - temp_heigh_kernel_start + 1)*(temp_width_kernel_end - temp_width_kernel_start + 1));
            //all3 += (in.at<Vec3b>(i, j)[2] - (int)vec[2])*(in.at<Vec3b>(i, j)[2] - (int)vec[2]) / ((temp_heigh_kernel_end - temp_heigh_kernel_start + 1)*(temp_width_kernel_end - temp_width_kernel_start + 1));
            all1 += (vec[0] - avg[0])*(vec[0] - avg[0]);
            all2 += (vec[1] - avg[1])*(vec[1] - avg[1]);
            all3 += (vec[2] - avg[2])*(vec[2] - avg[2]);
            
            //all3 += (vec - (int)avg)*(vec - (int)avg);
            y++;
        }
        x++;
    }
    Vec3d tmp;
    //int tmp;
    //cout << size << '\t' << "all1=" << all1 << '\t' << "all2" << all2 << '\t' << "all3" << all3 << endl;
    tmp[0] = all1/size;
    tmp[1] = all2/size;
    tmp[2] = all3/size;
    //tmp = all3 / size;
    return tmp;
}
Vec3b getAllavg(Mat src)
{
    //int all = 0;
    
    int r = 0;
    int g = 0;
    int b = 0;
    for (int i = 0; i != src.rows; ++i)
        for (int j = 0; j != src.cols; ++j)
        {
            r += src.at<Vec3b>(i, j)[2];
            g += src.at<Vec3b>(i, j)[1];
            b += src.at<Vec3b>(i, j)[0];
        }
    Vec3b tmp;
    tmp[0] = b / (src.rows*src.cols);
    tmp[1] = g / (src.rows*src.cols);
    tmp[2] = r / (src.rows*src.cols);
    return tmp;
    
    //for (int i = 0; i != src.rows; ++i)
        //for (int j = 0; j != src.cols; ++j)
        //{
    //        all += src.at<uchar>(i, j);
    //    }
    //return all / src.rows / src.cols;
}

const Vec3b& getAllvar(Mat src)
{
    Vec3b avg = getAllavg(src);
    Vec3b var;
    var[0] = 0;
    var[1] = 0;
    var[2] = 0;
    for (int i = 0; i != src.rows; ++i)
        for (int j = 0; j != src.cols; ++j)
        {
            var[0] = (src.at<Vec3b>(i, j)[0] - avg[0])*(src.at<Vec3b>(i, j)[0] - avg[0]);
            var[1] = (src.at<Vec3b>(i, j)[1] - avg[1])*(src.at<Vec3b>(i, j)[1] - avg[1]);
            var[2] = (src.at<Vec3b>(i, j)[2] - avg[2])*(src.at<Vec3b>(i, j)[2] - avg[2]);
        }
    var[0]=var[0] / src.rows / src.cols;
    var[1] = var[1] / src.rows / src.cols;
    var[2] = var[2] / src.rows / src.cols;
    return var;
}


void RGB2HSI(Mat src, Mat&dst)
{
    if (src.rows != dst.rows || src.cols != dst.cols)
        return;
    double theta = 0;
    double top = 0;
    double bottom = 0;
    for (int i = 0; i != src.rows; ++i)
        for (int j = 0; j != src.cols; ++j)
        {
            //cout << i << '\t' << j << endl;
            top = 0.5*(src.at<Vec3b>(i, j)[2] - src.at<Vec3b>(i, j)[1] + src.at<Vec3b>(i, j)[2] - src.at<Vec3b>(i, j)[0]);
            bottom = sqrt((src.at<Vec3b>(i, j)[2] - src.at<Vec3b>(i, j)[1])*(src.at<Vec3b>(i, j)[2] - src.at<Vec3b>(i, j)[1]) + (src.at<Vec3b>(i, j)[2] - src.at<Vec3b>(i, j)[0])*(src.at<Vec3b>(i, j)[1] - src.at<Vec3b>(i, j)[0]));
            theta = acos(top / bottom);
            theta = theta * 180 / 3.14;
            if (src.at<Vec3b>(i, j)[0] > src.at<Vec3b>(i, j)[1])
                dst.at<Vec3b>(i, j)[2] = 360 - theta;
            else
                dst.at<Vec3b>(i, j)[2] = theta;
            //cout << theta << endl;
            int min = src.at<Vec3b>(i, j)[2];
            if (src.at<Vec3b>(i, j)[1] < min)
                min = src.at<Vec3b>(i, j)[1];
            if (src.at<Vec3b>(i, j)[0] < min)
                min = src.at<Vec3b>(i, j)[0];
            dst.at<Vec3b>(i, j)[1] = 1 - (3 * min / (src.at<Vec3b>(i, j)[0] + src.at<Vec3b>(i, j)[1] + src.at<Vec3b>(i, j)[2]));
            dst.at<Vec3b>(i, j)[0] = (src.at<Vec3b>(i, j)[0] + src.at<Vec3b>(i, j)[1] + src.at<Vec3b>(i, j)[2]) / 3;
        }
}

int main()
{
    Mat src = imread("lena.jpg");
//    GaussianBlur(src, src, Size(3, 3), 1);
//    cvtColor(src, src, CV_BGR2GRAY);
    //cvtColor(src, src, CV_BGR2HLS);
    Mat copy,keep,his,tmp;
    Mat hsi;
    src.copyTo(hsi);
    src.copyTo(his);
    cvtColor(his, his, CV_BGR2GRAY);
    
    RGB2HSI(hsi, hsi);
    hsi.copyTo(tmp);
    src.copyTo(keep);
    src.copyTo(copy);
    mat_heigh = src.rows;
    mat_width = src.cols;
    int **kernel = new int*[kernel_heigh];
    for (int i = 0; i != kernel_heigh; ++i)
        kernel[i] = new int[kernel_width];
    for (int i = 0; i != kernel_heigh; ++i)
        for (int j = 0; j != kernel_width; ++j)
            kernel[i][j] = 1;
    //int avg;
    Vec3d var;
    var=getAllvar(src);
    cout << var << endl;
    //cout <<"avg"<< avg << endl;
    for (int i = 0; i != src.rows; ++i)
        for (int j = 0; j != src.cols; ++j)
        {
            Vec3d tmp;
        //    int tmp;
            tmp=getAvg(src, kernel, i, j);
        //    copy.at<uchar>(i, j) = tmp+/*((0.4*avg / getVar(src,tmp, i, j))*/5*(copy.at<uchar>(i,j)-tmp);
        //    cout << tmp << endl;
            for (int k = 0; k != 3; ++k)
                hsi.at<Vec3b>(i, j)[k] = tmp[k]+(var[k]*1.1/getVar(src, tmp, i, j)[k])*(hsi.at<Vec3b>(i,j)[k]-tmp[k]);

        }
    //cvtColor(keep, keep, CV_BGR2GRAY);
    imshow("1", keep);
    //cvtColor(copy, copy,CV_BGR2GRAY);
    imshow("local", copy);
    equalizeHist(his, his);
    imshow("before_hsi", tmp);
    imshow("after_hsi", hsi);
    imshow("his", his);
    waitKey(0);
}

 

#include<iostream>#include<cv.hpp>#include<highgui.hpp>using namespace std;using namespace cv;int pos_archor_width = 1;int pos_archor_heigh = 1;int mat_width;int mat_heigh;int kernel_width = 3;int kernel_heigh = 3;const Vec3b& getAvg(Mat in, int **kernel, int pos_heigh, int pos_width){int temp_width_kernel_start, temp_width_kernel_end;int temp_heigh_kernel_start, temp_heigh_kernel_end;if (pos_width >= pos_archor_width)temp_width_kernel_start = 0;elsetemp_width_kernel_start = pos_archor_width - pos_width;if (mat_width - 1 - pos_width >= kernel_width - 1 - pos_archor_width)temp_width_kernel_end = kernel_width - 1;elsetemp_width_kernel_end = mat_width - 1 - pos_width + pos_archor_width;if (pos_heigh >= pos_archor_heigh)temp_heigh_kernel_start = 0;elsetemp_heigh_kernel_start = pos_archor_heigh - pos_heigh;if (mat_heigh - 1 - pos_heigh >= kernel_heigh - 1 - pos_archor_heigh)temp_heigh_kernel_end = kernel_heigh - 1;elsetemp_heigh_kernel_end = mat_heigh - 1 - pos_heigh + pos_archor_heigh;
double all1 = 0;double all2 = 0;double all3 = 0;int x = 0;Vec3d vec;//int vec;int size =((temp_heigh_kernel_end - temp_heigh_kernel_start + 1)*(temp_width_kernel_end - temp_width_kernel_start + 1));//cout<<in.depth();for (int i = temp_heigh_kernel_start; i <= temp_heigh_kernel_end; ++i){int y = 0;
for (int j = temp_width_kernel_start; j <= temp_width_kernel_end; ++j){vec = in.at<Vec3b>((pos_heigh - (pos_archor_heigh - temp_heigh_kernel_start) + x), (pos_width - (pos_archor_width - temp_width_kernel_start) + y));all1 += (int)vec[0];all2 += (int)vec[1];all3 += (int)vec[2] ;//all3 += vec;
y++;}x++;}Vec3d tmp;//int tmp;tmp[0] = all1/size;tmp[1] = all2/size;tmp[2] = all3/size;//tmp = all3 / size;return tmp;}
const Vec3b& getVar(Mat in, Vec3d avg/*int avg*/, int pos_heigh, int pos_width){int temp_width_kernel_start, temp_width_kernel_end;int temp_heigh_kernel_start, temp_heigh_kernel_end;if (pos_width >= pos_archor_width)temp_width_kernel_start = 0;elsetemp_width_kernel_start = pos_archor_width - pos_width;if (mat_width - 1 - pos_width >= kernel_width - 1 - pos_archor_width)temp_width_kernel_end = kernel_width - 1;elsetemp_width_kernel_end = mat_width - 1 - pos_width + pos_archor_width;if (pos_heigh >= pos_archor_heigh)temp_heigh_kernel_start = 0;elsetemp_heigh_kernel_start = pos_archor_heigh - pos_heigh;if (mat_heigh - 1 - pos_heigh >= kernel_heigh - 1 - pos_archor_heigh)temp_heigh_kernel_end = kernel_heigh - 1;elsetemp_heigh_kernel_end = mat_heigh - 1 - pos_heigh + pos_archor_heigh;
double all1 = 0;double all2 = 0;double all3 = 0;int x = 0;Vec3d vec;//int vec;vec = avg;int size = 0;size = ((temp_heigh_kernel_end - temp_heigh_kernel_start+1)*(temp_width_kernel_end - temp_width_kernel_start+1));//cout<<in.depth();for (int i = temp_heigh_kernel_start; i <= temp_heigh_kernel_end; ++i){int y = 0;
for (int j = temp_width_kernel_start; j <= temp_width_kernel_end; ++j){//vec = in.at<Vec3b>((pos_heigh - (pos_archor_heigh - temp_heigh_kernel_start) + x), (pos_width - (pos_archor_width - temp_width_kernel_start) + y));vec = in.at<Vec3b>((pos_heigh - (pos_archor_heigh - temp_heigh_kernel_start) + x), (pos_width - (pos_archor_width - temp_width_kernel_start) + y));
//cout << vec << endl;//all1 += (in.at<Vec3b>(i,j)[0]- (int)vec[0])*(in.at<Vec3b>(i, j)[0] - (int)vec[0]) / ((temp_heigh_kernel_end - temp_heigh_kernel_start + 1)*(temp_width_kernel_end - temp_width_kernel_start + 1));//all2 += (in.at<Vec3b>(i, j)[1] - (int)vec[1])*(in.at<Vec3b>(i, j)[1] - (int)vec[1])/((temp_heigh_kernel_end - temp_heigh_kernel_start + 1)*(temp_width_kernel_end - temp_width_kernel_start + 1));//all3 += (in.at<Vec3b>(i, j)[2] - (int)vec[2])*(in.at<Vec3b>(i, j)[2] - (int)vec[2]) / ((temp_heigh_kernel_end - temp_heigh_kernel_start + 1)*(temp_width_kernel_end - temp_width_kernel_start + 1));all1 += (vec[0] - avg[0])*(vec[0] - avg[0]);all2 += (vec[1] - avg[1])*(vec[1] - avg[1]);all3 += (vec[2] - avg[2])*(vec[2] - avg[2]);//all3 += (vec - (int)avg)*(vec - (int)avg);y++;}x++;}Vec3d tmp;//int tmp;//cout << size << '\t' << "all1=" << all1 << '\t' << "all2" << all2 << '\t' << "all3" << all3 << endl;tmp[0] = all1/size;tmp[1] = all2/size;tmp[2] = all3/size;//tmp = all3 / size;return tmp;}Vec3b getAllavg(Mat src){//int all = 0;int r = 0;int g = 0;int b = 0;for (int i = 0; i != src.rows; ++i)for (int j = 0; j != src.cols; ++j){r += src.at<Vec3b>(i, j)[2];g += src.at<Vec3b>(i, j)[1];b += src.at<Vec3b>(i, j)[0];}Vec3b tmp;tmp[0] = b / (src.rows*src.cols);tmp[1] = g / (src.rows*src.cols);tmp[2] = r / (src.rows*src.cols);return tmp;//for (int i = 0; i != src.rows; ++i)//for (int j = 0; j != src.cols; ++j)//{//all += src.at<uchar>(i, j);//}//return all / src.rows / src.cols;}
const Vec3b& getAllvar(Mat src){Vec3b avg = getAllavg(src);Vec3b var;var[0] = 0;var[1] = 0;var[2] = 0;for (int i = 0; i != src.rows; ++i)for (int j = 0; j != src.cols; ++j){var[0] = (src.at<Vec3b>(i, j)[0] - avg[0])*(src.at<Vec3b>(i, j)[0] - avg[0]);var[1] = (src.at<Vec3b>(i, j)[1] - avg[1])*(src.at<Vec3b>(i, j)[1] - avg[1]);var[2] = (src.at<Vec3b>(i, j)[2] - avg[2])*(src.at<Vec3b>(i, j)[2] - avg[2]);}var[0]=var[0] / src.rows / src.cols;var[1] = var[1] / src.rows / src.cols;var[2] = var[2] / src.rows / src.cols;return var;}

void RGB2HSI(Mat src, Mat&dst){if (src.rows != dst.rows || src.cols != dst.cols)return;double theta = 0;double top = 0;double bottom = 0;for (int i = 0; i != src.rows; ++i)for (int j = 0; j != src.cols; ++j){//cout << i << '\t' << j << endl;top = 0.5*(src.at<Vec3b>(i, j)[2] - src.at<Vec3b>(i, j)[1] + src.at<Vec3b>(i, j)[2] - src.at<Vec3b>(i, j)[0]);bottom = sqrt((src.at<Vec3b>(i, j)[2] - src.at<Vec3b>(i, j)[1])*(src.at<Vec3b>(i, j)[2] - src.at<Vec3b>(i, j)[1]) + (src.at<Vec3b>(i, j)[2] - src.at<Vec3b>(i, j)[0])*(src.at<Vec3b>(i, j)[1] - src.at<Vec3b>(i, j)[0]));theta = acos(top / bottom);theta = theta * 180 / 3.14;if (src.at<Vec3b>(i, j)[0] > src.at<Vec3b>(i, j)[1])dst.at<Vec3b>(i, j)[2] = 360 - theta;elsedst.at<Vec3b>(i, j)[2] = theta;//cout << theta << endl;int min = src.at<Vec3b>(i, j)[2];if (src.at<Vec3b>(i, j)[1] < min)min = src.at<Vec3b>(i, j)[1];if (src.at<Vec3b>(i, j)[0] < min)min = src.at<Vec3b>(i, j)[0];dst.at<Vec3b>(i, j)[1] = 1 - (3 * min / (src.at<Vec3b>(i, j)[0] + src.at<Vec3b>(i, j)[1] + src.at<Vec3b>(i, j)[2]));dst.at<Vec3b>(i, j)[0] = (src.at<Vec3b>(i, j)[0] + src.at<Vec3b>(i, j)[1] + src.at<Vec3b>(i, j)[2]) / 3;}}
int main(){Mat src = imread("lena.jpg");//GaussianBlur(src, src, Size(3, 3), 1);//cvtColor(src, src, CV_BGR2GRAY);//cvtColor(src, src, CV_BGR2HLS);Mat copy,keep,his,tmp;Mat hsi;src.copyTo(hsi);src.copyTo(his);cvtColor(his, his, CV_BGR2GRAY);RGB2HSI(hsi, hsi);hsi.copyTo(tmp);src.copyTo(keep);src.copyTo(copy);mat_heigh = src.rows;mat_width = src.cols;int **kernel = new int*[kernel_heigh];for (int i = 0; i != kernel_heigh; ++i)kernel[i] = new int[kernel_width];for (int i = 0; i != kernel_heigh; ++i)for (int j = 0; j != kernel_width; ++j)kernel[i][j] = 1;//int avg;Vec3d var;var=getAllvar(src);cout << var << endl;//cout <<"avg"<< avg << endl;for (int i = 0; i != src.rows; ++i)for (int j = 0; j != src.cols; ++j){Vec3d tmp;//int tmp;tmp=getAvg(src, kernel, i, j);//copy.at<uchar>(i, j) = tmp+/*((0.4*avg / getVar(src,tmp, i, j))*/5*(copy.at<uchar>(i,j)-tmp);//cout << tmp << endl;for (int k = 0; k != 3; ++k)hsi.at<Vec3b>(i, j)[k] = tmp[k]+(var[k]*1.1/getVar(src, tmp, i, j)[k])*(hsi.at<Vec3b>(i,j)[k]-tmp[k]);
}//cvtColor(keep, keep, CV_BGR2GRAY);imshow("1", keep);//cvtColor(copy, copy,CV_BGR2GRAY);imshow("local", copy);equalizeHist(his, his);imshow("before_hsi", tmp);imshow("after_hsi", hsi);imshow("his", his);waitKey(0);}

posted @ 2016-04-13 17:23  你猜你猜啊  阅读(140)  评论(0编辑  收藏  举报