Image Adjustment
2016-06-13 12:48
330 查看
Image adjustment generally includes level, contrast, gamma, hue, saturation and brightness modification.
In Matlab, you can use the func imadjust()% read the image
I = imread("./pout.tif");
imshow();
% Adjust the contrast the image so that 1% of the data is saturated at low and high intensities, and display it
J = imadjust(I);
figure
imshow(J)There is no builtin solution in OpenCV to perform it, but it can be easily realized in a loop.
#include <opencv2/opencv.hpp>
#include <vector>
#include <algorithm>
using namespace std;
using namespace cv;
void imadjust(const Mat1b& src, Mat1b& dst, int tol = 1, Vec2i in = Vec2i(0, 255), Vec2i out = Vec2i(0, 255))
{
// src: input CV_8UC1 image
// dst: output CV_8UC1 image
// tol: tolerance, from 0 to 100.
// in: src image bounds
// out: dst image bounds
dst = src.clone();
tol = max(0, min(100, tol));
if (tol > 0)
{
// Histogram
vector<int> hist(256, 0);
for (int r = 0; r < src.rows; r++) {
for (int c = 0; c < src.cols; c++) {
hist[src(r, c)]++;
}
}
// Cumulative histogram
vector<int> cum = hist;
for (int i = 1; i < hist.size(); i++)
cum[i] = cum[i - 1] + hist[i];
// Compute bounds
int total = src.rows * src.cols;
int low_bound = total * tol / 100;
int up_bound = total * (100 - tol) / 100;
in[0] = distance(cum.begin(), lower_bound(cum.begin(), cum.end(), low_bound));
in[1] = distance(cum.begin(), lower_bound(cum.begin(), cum.end(), up_bound));
}
// Stretching
float scale = float(out[1] - out[0]) / float(in[1] - in[0]);
for (int r = 0; r < dst.rows; r++) {
for (int c = 0; c < dst.cols; c++) {
int vs = max(src(r, c) - in[0], 0);
int vd = min(int(vs * scale + 0.5f) + out[0], out[1]);
dst(r, c) = saturate_cast<uchar>(vd);
}
}
}
int main()
{
char img_file[256] = "./imadjust1.png";
Mat3b img = imread(img_file);
if (!img.data) {
printf("Warning: the image [%s] is empty!\n", img_file);
}
return 0;
Mat1b gray;
cvtColor(img, gray, COLOR_RGB2GRAY);
Mat1b adjusted;
imadjust(gray, adjusted);
imwrite("./adjusted.png", adjusted);
return 0;
}
In Matlab, you can use the func imadjust()% read the image
I = imread("./pout.tif");
imshow();
% Adjust the contrast the image so that 1% of the data is saturated at low and high intensities, and display it
J = imadjust(I);
figure
imshow(J)There is no builtin solution in OpenCV to perform it, but it can be easily realized in a loop.
#include <opencv2/opencv.hpp>
#include <vector>
#include <algorithm>
using namespace std;
using namespace cv;
void imadjust(const Mat1b& src, Mat1b& dst, int tol = 1, Vec2i in = Vec2i(0, 255), Vec2i out = Vec2i(0, 255))
{
// src: input CV_8UC1 image
// dst: output CV_8UC1 image
// tol: tolerance, from 0 to 100.
// in: src image bounds
// out: dst image bounds
dst = src.clone();
tol = max(0, min(100, tol));
if (tol > 0)
{
// Histogram
vector<int> hist(256, 0);
for (int r = 0; r < src.rows; r++) {
for (int c = 0; c < src.cols; c++) {
hist[src(r, c)]++;
}
}
// Cumulative histogram
vector<int> cum = hist;
for (int i = 1; i < hist.size(); i++)
cum[i] = cum[i - 1] + hist[i];
// Compute bounds
int total = src.rows * src.cols;
int low_bound = total * tol / 100;
int up_bound = total * (100 - tol) / 100;
in[0] = distance(cum.begin(), lower_bound(cum.begin(), cum.end(), low_bound));
in[1] = distance(cum.begin(), lower_bound(cum.begin(), cum.end(), up_bound));
}
// Stretching
float scale = float(out[1] - out[0]) / float(in[1] - in[0]);
for (int r = 0; r < dst.rows; r++) {
for (int c = 0; c < dst.cols; c++) {
int vs = max(src(r, c) - in[0], 0);
int vd = min(int(vs * scale + 0.5f) + out[0], out[1]);
dst(r, c) = saturate_cast<uchar>(vd);
}
}
}
int main()
{
char img_file[256] = "./imadjust1.png";
Mat3b img = imread(img_file);
if (!img.data) {
printf("Warning: the image [%s] is empty!\n", img_file);
}
return 0;
Mat1b gray;
cvtColor(img, gray, COLOR_RGB2GRAY);
Mat1b adjusted;
imadjust(gray, adjusted);
imwrite("./adjusted.png", adjusted);
return 0;
}
相关文章推荐
- PHP GD 图像处理组件的常用函数总结
- PHP图像处理之imagecreate、imagedestroy函数介绍
- jsvascript图像处理―(计算机视觉应用)图像金字塔
- Javascript图像处理思路及实现代码
- PHP图像处理之使用imagecolorallocate()函数设置颜色例子
- java数字图像处理基础使用imageio写图像文件示例
- 使用Java进行图像处理的一些基础操作
- javascript图像处理―边缘梯度计算函数
- Javascript图像处理―阈值函数实例应用
- Javascript图像处理―虚拟边缘介绍及使用方法
- PHP图像处理类库及演示分享
- CI框架文件上传类及图像处理类用法分析
- php图像处理函数大全(推荐收藏)
- Javascript图像处理―图像形态学(膨胀与腐蚀)
- Javascript图像处理―平滑处理实现原理
- Swift图像处理之优化照片
- 在Ubuntu上安装OpenCV3.0和Python-openCV的经历
- CI框架文件上传类及图像处理类用法分析
- VTK学习笔记之图像处理
- vtk 图像处理 多种 操作