Opencv-Trackbar实现图像亮度和对比度调整(创建两个Trackbar共同作用于同一张图片)

Opencv4.6版本,封装好的亮度和对比度类

  • 1.AdjustImage类(实现亮度、对比度封装)
    • AdjustImage.h
    • AdjustImage.cpp
    • Main.cpp
    • 结果(可调节两个trackBar来控制一张图片的亮度和对比度)

1.AdjustImage类(实现亮度、对比度封装)

Opencv安装教程,可以参考安装opencv。

AdjustImage.h

#pragma once
#include 
#include 

using namespace cv;
using namespace std;

class AdjustImage {

	private:
	  int colorStart;//初始亮度
	  int contrast;//初始对比度
	  int colorMax;//亮度最大值
	  int contrastMax;//对比度最大值
      Mat src;//原图像
      string nameWin;//窗口名称 
      string trackName1;//亮度bar
      string trackName2;//对比度bar
      
	public:
          AdjustImage();//空构造
         AdjustImage(Mat src, int colorStart, int contrast, int colorMax,
                      int contrastMax, string nameWin, string trackName1,
                     string trackName2);
          int getColorStart();
          int getContrast();
          int getColorMax();
          int getContrastMax();
          string getNameWin();
          string getTrackName1();
          string getTrackName2();
          void setColorStart(int colorStart);
          void setContrast(int contrast);
          Mat getSrc();
          void trackbarInit( AdjustImage adjust);//初始化
          
};

AdjustImage.cpp

#include "iostream"

#include "AdjustImage.h"



AdjustImage::AdjustImage() {}

AdjustImage::AdjustImage(Mat src, int colorStart, int contrast, int colorMax,
                         int contrastMax, string nameWin, string trackName1,
                         string trackName2) {
  this->src = src;
  this->contrast = colorStart;
  this->contrast = contrast;
  this->colorMax = colorMax;
  this->contrastMax = contrastMax;
  this->nameWin = nameWin;
  this->trackName1 = trackName1;
  this->trackName2 = trackName2;
}

int AdjustImage::getColorStart() { return this->colorStart; }

int AdjustImage::getContrast() { return this->contrast; }

int AdjustImage::getColorMax() { return this->colorMax; }

int AdjustImage::getContrastMax() { return this->contrastMax; }

Mat AdjustImage::getSrc() { return this->src; }

string AdjustImage::getNameWin() { return this->nameWin; }
string AdjustImage::getTrackName1() { return this->trackName1; }
string AdjustImage::getTrackName2() { return this->trackName2; }

void AdjustImage::setColorStart(int colorStart) {
  this->colorStart = colorStart;
}

void AdjustImage::setContrast(int contrast) { this->contrast = contrast; }

void setContrastMax(int contrast);



void mytrackBarCallback(int, void* data) {
  AdjustImage adjust = *(AdjustImage*)(data);
  string nameWin = adjust.getNameWin();
  string lightnessTrack = adjust.getTrackName1();
  string contrastTrack = adjust.getTrackName2();
  adjust.setColorStart(getTrackbarPos(lightnessTrack, nameWin));
  adjust.setContrast(getTrackbarPos(contrastTrack, nameWin));

  Mat srcBar = adjust.getSrc();
  Mat dst;
  srcBar.convertTo(dst, -1, adjust.getContrast() * 0.01, adjust.getColorStart() );
  imshow(nameWin, dst);
  waitKey(0);
}

void AdjustImage::trackbarInit(AdjustImage adjust) {
  string nameWin = adjust.getNameWin();
  string lightnessTrack = adjust.getTrackName1();
  string contrastTrack = adjust.getTrackName2();
  namedWindow(nameWin, WINDOW_AUTOSIZE);
  int colorStart = adjust.getColorStart();
  int colorMax = adjust.getColorMax();
  int contrast = adjust.getContrast();
  int contrastMax = adjust.getContrastMax();
  //调整亮度的TrackBar
  createTrackbar(lightnessTrack, nameWin, &colorStart, colorMax,
                 mytrackBarCallback, &adjust);
  //调整对比度的TrackBar,他们使用同一个回调函数
  createTrackbar(contrastTrack, nameWin, &contrast, contrastMax,
                 mytrackBarCallback, &adjust);
}

Main.cpp

#include 
#include 

#include"AdjustImage.h"
int main() {
  // 源图像载入及判断
  /*Mat srcImage = cv::imread("D:\\project\\defect-detection\\images\\guo\\test0.jpg");*/
  Mat srcImage =
      cv::imread("C:\\Users\\w000628\\Desktop\\2.png");
  if (!srcImage.data) {
    cout << "Failed to load image" << std::endl;
    return -1;
  }
  AdjustImage adjust =AdjustImage(srcImage, 0, 30, 255, 100, "adjust-image", "lightness", "contrast");
  AdjustImage op = AdjustImage();
  op.trackbarInit(adjust);
  return 0;
}

结果(可调节两个trackBar来控制一张图片的亮度和对比度)

你可能感兴趣的:(opencv,计算机视觉,c++)