• 大小: 2.77MB
    文件类型: .7z
    金币: 1
    下载: 0 次
    发布日期: 2023-09-08
  • 语言: 其他
  • 标签: 颜色增强  opencv  c++  

资源简介

opencv实现图像颜色增强算法,vs2013+opencv2.4.13 实现。、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、

资源截图

代码片段和文件信息

#include
#include
using namespace std;
using namespace cv;

/*
Stretch color saturation to cover maximum possible range“
“This simple plug-in does an automatic saturation stretch.
For each channel in the image it finds the minimum and maximum values...
it uses those values to stretch the individual histograms to the full range.
For some images it may do just what you want; for others it may not work that well.
This version differs from Contrast Autostretch in that it works in HSV space and preserves hue.

*/

double vdelta = 200;
double stheta = 30;

void find_vhi_vlo(const Mat &img double &vhi double &vlo)
{
int width = img.cols;
int height = img.rows;

uchar min;

vector BGR;
split(img BGR);
//conver to CMY
BGR[0] = 255 - BGR[0];
BGR[1] = 255 - BGR[1];
BGR[2] = 255 - BGR[2];

Mat CMY(img.size() CV_8UC3);
merge(BGR CMY);


for (int i = 0; i < height; i++)
{
Vec3b *pImg = CMY.ptr(i);
for (int j = 0; j < width; j++)
{
min = pImg[j][0];
if (pImg[j][1] < min) min = pImg[j][1];
if (pImg[j][2] < min) min = pImg[j][2];
for (int k = 0; k < 3; k++)
{
pImg[j][k] -= min;


}
}
}

Mat HSV = Mat(CMY.size() CV_8UC3);
//imshow(“cmy“CMY);
cvtColor(CMY HSV COLOR_BGR2HSV);


vector vHSV;

split(HSV vHSV);

//
////find Vmin and Vmax

double vMin = 1.0;
double vMax = .0;

for (int i = 0; i < height; i++)
{
uchar *pImg = vHSV[2].ptr(i);
for (int j = 0; j < width; j++)
{
double v = (double)pImg[j] / 255.0;

if (v > vMax) vMax = v;
if (v < vMin) vMin = v;
}
}

vhi = vMax;
vlo = vMin;

}
//v线性量化
//img单通道
void quantizing_v(Mat &img double vMax double vMin)
{
if (vMax == vMin) return;
if (img.channels() != 1) return;

int width = img.cols;
int height = img.rows;

double delta = 300.0;
for (int i = 0; i < height; i++)
{
uchar *pImg = img.ptr(i);
for (int j = 0; j < width; j++)
{
//double oldPixel = (double)pImg[j];
double newPixel = ((double)pImg[j] / 255 - vMin) / (vMax - vMin);
int tmp = int(newPixel * delta);
if (tmp >255)
pImg[j] = 255;
else if (tmp < 0)
pImg[j] = 0;
else
pImg[j] = (uchar)tmp;

}
}
//cout << “new img“ << endl << img << endl;
}



void color_enhance(const Mat &img Mat &dst double vMax double vMin)
{
double v;//v量化

int width = img.cols;
int height = img.rows;

uchar min;
vector BGR;
split(img BGR);
//equalizeHist(BGR[2]BGR[2]);

//conver to CMY
BGR[0] = 255 - BGR[0];
BGR[1] = 255 - BGR[1];
BGR[2] = 255 - BGR[2];

Mat CMY(img.size() CV_8UC3);
merge(BGR CMY);

//imshow(“cmy1“ CMY);

Mat minMat(img.size() CV_8UC1);

for (int i = 0; i < height; i++)
{
Vec3b *pImg = CMY.ptr(i);
uchar *pMin = minMat.ptr(i);

for (int j = 0; j < width; j++)
{
min = pImg[j][0];

评论

共有 条评论