1
0
mirror of https://github.com/opencv/opencv.git synced 2026-07-25 13:23:02 +04:00

Add Java and Python code for the following tutorials:

- Changing the contrast and brightness of an image!
      - Operations with images
This commit is contained in:
catree
2018-07-11 19:38:16 +02:00
parent a29d11240e
commit c9fe6f1afe
12 changed files with 1197 additions and 166 deletions
@@ -20,29 +20,32 @@ using namespace cv;
*/
int main( int argc, char** argv )
{
//! [basic-linear-transform-parameters]
double alpha = 1.0; /*< Simple contrast control */
int beta = 0; /*< Simple brightness control */
//! [basic-linear-transform-parameters]
/// Read image given by user
//! [basic-linear-transform-load]
String imageName("../data/lena.jpg"); // by default
if (argc > 1)
CommandLineParser parser( argc, argv, "{@input | ../data/lena.jpg | input image}" );
Mat image = imread( parser.get<String>( "@input" ) );
if( image.empty() )
{
imageName = argv[1];
cout << "Could not open or find the image!\n" << endl;
cout << "Usage: " << argv[0] << " <Input image>" << endl;
return -1;
}
Mat image = imread( imageName );
//! [basic-linear-transform-load]
//! [basic-linear-transform-output]
Mat new_image = Mat::zeros( image.size(), image.type() );
//! [basic-linear-transform-output]
//! [basic-linear-transform-parameters]
double alpha = 1.0; /*< Simple contrast control */
int beta = 0; /*< Simple brightness control */
/// Initialize values
cout << " Basic Linear Transforms " << endl;
cout << "-------------------------" << endl;
cout << "* Enter the alpha value [1.0-3.0]: "; cin >> alpha;
cout << "* Enter the beta value [0-100]: "; cin >> beta;
//! [basic-linear-transform-parameters]
/// Do the operation new_image(i,j) = alpha*image(i,j) + beta
/// Instead of these 'for' loops we could have used simply:
@@ -51,19 +54,15 @@ int main( int argc, char** argv )
//! [basic-linear-transform-operation]
for( int y = 0; y < image.rows; y++ ) {
for( int x = 0; x < image.cols; x++ ) {
for( int c = 0; c < 3; c++ ) {
for( int c = 0; c < image.channels(); c++ ) {
new_image.at<Vec3b>(y,x)[c] =
saturate_cast<uchar>( alpha*( image.at<Vec3b>(y,x)[c] ) + beta );
saturate_cast<uchar>( alpha*image.at<Vec3b>(y,x)[c] + beta );
}
}
}
//! [basic-linear-transform-operation]
//! [basic-linear-transform-display]
/// Create Windows
namedWindow("Original Image", WINDOW_AUTOSIZE);
namedWindow("New Image", WINDOW_AUTOSIZE);
/// Show stuff
imshow("Original Image", image);
imshow("New Image", new_image);
@@ -3,6 +3,8 @@
#include "opencv2/highgui.hpp"
// we're NOT "using namespace std;" here, to avoid collisions between the beta variable and std::beta in c++17
using std::cout;
using std::endl;
using namespace cv;
namespace
@@ -19,12 +21,13 @@ void basicLinearTransform(const Mat &img, const double alpha_, const int beta_)
img.convertTo(res, -1, alpha_, beta_);
hconcat(img, res, img_corrected);
imshow("Brightness and contrast adjustments", img_corrected);
}
void gammaCorrection(const Mat &img, const double gamma_)
{
CV_Assert(gamma_ >= 0);
//![changing-contrast-brightness-gamma-correction]
//! [changing-contrast-brightness-gamma-correction]
Mat lookUpTable(1, 256, CV_8U);
uchar* p = lookUpTable.ptr();
for( int i = 0; i < 256; ++i)
@@ -32,9 +35,10 @@ void gammaCorrection(const Mat &img, const double gamma_)
Mat res = img.clone();
LUT(img, lookUpTable, res);
//![changing-contrast-brightness-gamma-correction]
//! [changing-contrast-brightness-gamma-correction]
hconcat(img, res, img_gamma_corrected);
imshow("Gamma correction", img_gamma_corrected);
}
void on_linear_transform_alpha_trackbar(int, void *)
@@ -60,36 +64,32 @@ void on_gamma_correction_trackbar(int, void *)
int main( int argc, char** argv )
{
String imageName("../data/lena.jpg"); // by default
if (argc > 1)
CommandLineParser parser( argc, argv, "{@input | ../data/lena.jpg | input image}" );
img_original = imread( parser.get<String>( "@input" ) );
if( img_original.empty() )
{
imageName = argv[1];
cout << "Could not open or find the image!\n" << endl;
cout << "Usage: " << argv[0] << " <Input image>" << endl;
return -1;
}
img_original = imread( imageName );
img_corrected = Mat(img_original.rows, img_original.cols*2, img_original.type());
img_gamma_corrected = Mat(img_original.rows, img_original.cols*2, img_original.type());
hconcat(img_original, img_original, img_corrected);
hconcat(img_original, img_original, img_gamma_corrected);
namedWindow("Brightness and contrast adjustments", WINDOW_AUTOSIZE);
namedWindow("Gamma correction", WINDOW_AUTOSIZE);
namedWindow("Brightness and contrast adjustments");
namedWindow("Gamma correction");
createTrackbar("Alpha gain (contrast)", "Brightness and contrast adjustments", &alpha, 500, on_linear_transform_alpha_trackbar);
createTrackbar("Beta bias (brightness)", "Brightness and contrast adjustments", &beta, 200, on_linear_transform_beta_trackbar);
createTrackbar("Gamma correction", "Gamma correction", &gamma_cor, 200, on_gamma_correction_trackbar);
while (true)
{
imshow("Brightness and contrast adjustments", img_corrected);
imshow("Gamma correction", img_gamma_corrected);
on_linear_transform_alpha_trackbar(0, 0);
on_gamma_correction_trackbar(0, 0);
int c = waitKey(30);
if (c == 27)
break;
}
waitKey();
imwrite("linear_transform_correction.png", img_corrected);
imwrite("gamma_correction.png", img_gamma_corrected);
@@ -0,0 +1,180 @@
/* Snippet code for Operations with images tutorial (not intended to be run but should built successfully) */
#include "opencv2/core.hpp"
#include "opencv2/core/core_c.h"
#include "opencv2/imgcodecs.hpp"
#include "opencv2/imgproc.hpp"
#include "opencv2/highgui.hpp"
#include <iostream>
using namespace cv;
using namespace std;
int main(int,char**)
{
std::string filename = "";
// Input/Output
{
//! [Load an image from a file]
Mat img = imread(filename);
//! [Load an image from a file]
CV_UNUSED(img);
}
{
//! [Load an image from a file in grayscale]
Mat img = imread(filename, IMREAD_GRAYSCALE);
//! [Load an image from a file in grayscale]
CV_UNUSED(img);
}
{
Mat img(4,4,CV_8U);
//! [Save image]
imwrite(filename, img);
//! [Save image]
}
// Accessing pixel intensity values
{
Mat img(4,4,CV_8U);
int y = 0, x = 0;
{
//! [Pixel access 1]
Scalar intensity = img.at<uchar>(y, x);
//! [Pixel access 1]
CV_UNUSED(intensity);
}
{
//! [Pixel access 2]
Scalar intensity = img.at<uchar>(Point(x, y));
//! [Pixel access 2]
CV_UNUSED(intensity);
}
{
//! [Pixel access 3]
Vec3b intensity = img.at<Vec3b>(y, x);
uchar blue = intensity.val[0];
uchar green = intensity.val[1];
uchar red = intensity.val[2];
//! [Pixel access 3]
CV_UNUSED(blue);
CV_UNUSED(green);
CV_UNUSED(red);
}
{
//! [Pixel access 4]
Vec3f intensity = img.at<Vec3f>(y, x);
float blue = intensity.val[0];
float green = intensity.val[1];
float red = intensity.val[2];
//! [Pixel access 4]
CV_UNUSED(blue);
CV_UNUSED(green);
CV_UNUSED(red);
}
{
//! [Pixel access 5]
img.at<uchar>(y, x) = 128;
//! [Pixel access 5]
}
{
int i = 0;
//! [Mat from points vector]
vector<Point2f> points;
//... fill the array
Mat pointsMat = Mat(points);
//! [Mat from points vector]
//! [Point access]
Point2f point = pointsMat.at<Point2f>(i, 0);
//! [Point access]
CV_UNUSED(point);
}
}
// Memory management and reference counting
{
//! [Reference counting 1]
std::vector<Point3f> points;
// .. fill the array
Mat pointsMat = Mat(points).reshape(1);
//! [Reference counting 1]
CV_UNUSED(pointsMat);
}
{
//! [Reference counting 2]
Mat img = imread("image.jpg");
Mat img1 = img.clone();
//! [Reference counting 2]
CV_UNUSED(img1);
}
{
//! [Reference counting 3]
Mat img = imread("image.jpg");
Mat sobelx;
Sobel(img, sobelx, CV_32F, 1, 0);
//! [Reference counting 3]
}
// Primitive operations
{
Mat img;
{
//! [Set image to black]
img = Scalar(0);
//! [Set image to black]
}
{
//! [Select ROI]
Rect r(10, 10, 100, 100);
Mat smallImg = img(r);
//! [Select ROI]
CV_UNUSED(smallImg);
}
}
{
//! [C-API conversion]
Mat img = imread("image.jpg");
IplImage img1 = img;
CvMat m = img;
//! [C-API conversion]
CV_UNUSED(img1);
CV_UNUSED(m);
}
{
//! [BGR to Gray]
Mat img = imread("image.jpg"); // loading a 8UC3 image
Mat grey;
cvtColor(img, grey, COLOR_BGR2GRAY);
//! [BGR to Gray]
}
{
Mat dst, src;
//! [Convert to CV_32F]
src.convertTo(dst, CV_32F);
//! [Convert to CV_32F]
}
// Visualizing images
{
//! [imshow 1]
Mat img = imread("image.jpg");
namedWindow("image", WINDOW_AUTOSIZE);
imshow("image", img);
waitKey();
//! [imshow 1]
}
{
//! [imshow 2]
Mat img = imread("image.jpg");
Mat grey;
cvtColor(img, grey, COLOR_BGR2GRAY);
Mat sobelx;
Sobel(grey, sobelx, CV_32F, 1, 0);
double minVal, maxVal;
minMaxLoc(sobelx, &minVal, &maxVal); //find minimum and maximum intensities
Mat draw;
sobelx.convertTo(draw, CV_8U, 255.0/(maxVal - minVal), -minVal * 255.0/(maxVal - minVal));
namedWindow("image", WINDOW_AUTOSIZE);
imshow("image", draw);
waitKey();
//! [imshow 2]
}
return 0;
}