Opencv 图像增强


前言

通过滑块调整图像的对比度和亮度。输出图像像素 = 对比度参数 * 原图像像素 +亮度参数 。通过两个花开改变两个参数进行调节。


一、使用步骤

1.主函数

代码如下(示例):

#include <iostream>
#include<opencv2/opencv.hpp>
#define _CRT_SECURE_NO_WARNINGS
using namespace std;
using namespace cv;

const int MaxValueIntensity = 300;
const int MaxValueContrast = 200;

int Intensity = 20; //对比度参数
int Contrast = 20;
void On_Intensity(int, void*);
Mat Img;

int main() {
    Mat Img2, Img3, HSV, Imgs[3], HSV0, HSV1, HSV2, Im_color;
    char IntensityControl[50]; //滑块字符
    char ContrastControl[50];  //滑块字符
    //加载图片
    Img = imread("D:/test/homework/1.jpg");
    Img2 = imread("D:/test/homework/2.bmp");
    Img3 = imread("D:/test/homework/3.jpg", IMREAD_GRAYSCALE);
    //创建串口
    namedWindow("ImgWindow", WINDOW_AUTOSIZE);

	sprintf_s(IntensityControl, "IntensityControl:%d", MaxValueIntensity);
    sprintf_s(ContrastControl, "ContrastControl:%d", MaxValueContrast);
    //创建滑块
    createTrackbar(IntensityControl, "ImgWindow", &Intensity, MaxValueIntensity, On_Intensity);
    createTrackbar(ContrastControl, "ImgWindow", &Contrast, MaxValueContrast, On_Intensity);

    //将rgb图片转换为HSB图片
    cvtColor(Img2, HSV, COLOR_BGR2HSV);
    //将三通道图片进行分割为三个单通道
    split(HSV, Imgs);
    //单通道显示
    HSV0 = Imgs[0];
    HSV1 = Imgs[1];
    HSV2 = Imgs[2];
    //三通道合并
    applyColorMap(Img3, Im_color, COLORMAP_JET);

    waitKey(0);
	return 0;
}

2.对比度回调函数

void On_Intensity(int, void*)
{
    int height = Img.rows;  //行数
    int width = Img.cols;  //列数
    Mat dst = Mat::zeros(Img.size(), Img.type());
    for (int y = 0; y < height; y++) {
        for (int x = 0; x < width; x++) {
            dst.at<Vec3b>(y, x)[0] = saturate_cast<uchar>((1.0 * Intensity / 100) * Img.at<Vec3b>(y, x)[0] + Contrast);//红色
            dst.at<Vec3b>(y, x)[1] = saturate_cast<uchar>((1.0 * Intensity / 100) * Img.at<Vec3b>(y, x)[1] + Contrast);//绿色
            dst.at<Vec3b>(y, x)[2] = saturate_cast<uchar>((1.0 * Intensity / 100) * Img.at<Vec3b>(y, x)[2] + Contrast);//蓝色
        }
    }
    imshow("ImgWindow",dst);
}

3. 亮度回调函数

void On_Contrast(int, void*)
{
    int height = Img.rows;  //行数
    int width = Img.cols;  //列数
    Mat dst = Mat::zeros(Img.size(), Img.type());
    for (int y = 0; y < height; y++) {
        for (int x = 0; x < width; x++) {
            dst.at<Vec3b>(y, x)[0] = saturate_cast<uchar>((1.0 * Intensity / 100) * Img.at<Vec3b>(y, x)[0] + Contrast);//红色
            dst.at<Vec3b>(y, x)[1] = saturate_cast<uchar>((1.0 * Intensity / 100) * Img.at<Vec3b>(y, x)[1] + Contrast);//绿色
            dst.at<Vec3b>(y, x)[2] = saturate_cast<uchar>((1.0 * Intensity / 100) * Img.at<Vec3b>(y, x)[2] + Contrast);//蓝色
        }
    }
    imshow("ImgWindow", dst);
}

总结

图像入门

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
OpenCV提供了多种图像增强方法,其中包括直方图均衡化和局部直方图均衡化。 直方图均衡化是一种将图像的像素值进行重新分布的方法,以增强图像的对比度。在OpenCV中,可以使用cv2.equalizeHist()函数来执行直方图均衡化。 局部直方图均衡化是在图像的不同区域应用不同的直方图均衡化方法,以处理不同区域之间的对比度差异。在OpenCV中,可以使用createCLAHE()函数来创建一个CLAHE对象,并使用apply()函数将其应用于图像。 除了直方图均衡化,OpenCV还提供了其他常用的图像增强方法,例如左右翻转、随机裁剪和颜色变换。可以使用cv2.flip()函数进行左右翻转,使用cv2.resize()函数进行随机裁剪,并使用cv2.cvtColor()函数进行颜色变换。你也可以使用cv2.add()函数来调整亮度,cv2.multiply()函数来调整对比度,cv2.cvtColor()函数来调整饱和度和色调。 下面是一个示例代码,展示了如何使用OpenCV实现部分图像增强方法: ```python import cv2 import numpy as np from matplotlib import pyplot as plt # Load an image from a file filename = 'work/1.jpg' img = cv2.imread(filename) img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) # Left-right flip flip_img = cv2.flip(img, 1) # Random crop crop_x = np.random.randint(0, img.shape - desired_width) crop_y = np.random.randint(0, img.shape - desired_height) crop_img = img[crop_y:crop_y+desired_height, crop_x:crop_x+desired_width] # Adjust brightness brightness_img = cv2.add(img, brightness_value) # Adjust contrast contrast_img = cv2.multiply(img, contrast_value) # Adjust saturation and hue hsv_img = cv2.cvtColor(img, cv2.COLOR_RGB2HSV) hsv_img[:,:,1 = cv2.multiply(hsv_img[:,:,1], saturation_value) hsv_img[:,:,0 = cv2.add(hsv_img[:,:,0], hue_value) saturation_hue_img = cv2.cvtColor(hsv_img, cv2.COLOR_HSV2RGB) # Display the images plt.figure(figsize=(12, 8)) plt.subplot(231), plt.imshow(img), plt.title('Original') plt.subplot(232), plt.imshow(flip_img), plt.title('Left-right flip') plt.subplot(233), plt.imshow(crop_img), plt.title('Random crop') plt.subplot(234), plt.imshow(brightness_img), plt.title('Adjust brightness') plt.subplot(235), plt.imshow(contrast_img), plt.title('Adjust contrast') plt.subplot(236), plt.imshow(saturation_hue_img), plt.title('Adjust saturation and hue') plt.show() ``` 这个示例代码展示了如何使用OpenCV实现图像增强的一些常见方法,包括左右翻转、随机裁剪和调整亮度、对比度、饱和度和色调。 希望这个回答能够满足你的需求。如果还有其他问题,请随时提问。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

LI_ER_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值