0% found this document useful (0 votes)
65 views4 pages

Mii Opencv

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
65 views4 pages

Mii Opencv

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

#include <stdio.

h>
using namespace std;

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

Mat srcImage; // input (source) image


Mat greyImage;
int threshold_value = 100;
int max_value = 255;
Mat binarizedImage;
const int histSize = 256;
const int hist_w = 256;
const int hist_h = 256;
float range[2] = { 0, 256 }; // zakres wartości histogramu (min; max - 1)
const float* histRange[] = { range };
void DrawHistogramAt(Mat src, const char* name, int x, int y)
{

}
void Threshold(int pos, void* userdata) {
threshold(srcImage, binarizedImage, threshold_value, max_value, THRESH_BINARY);
imshow("Binarization", binarizedImage);

void CreateWindow(const char* name, int x, int y)


{
namedWindow(name, WINDOW_AUTOSIZE);
moveWindow(name, x, y);

}
void ShowImage(Mat img, const char* name, int x, int y)
{
CreateWindow(name, x, y);
imshow(name, img);
}
void Brightness(Mat src, Mat dst, int B) {
for (int i = 0; i < src.rows; i++) {
for (int j = 0; j < src.cols; j++) {
Vec3b PixelColor = src.at<Vec3b>(i, j);
for (int k = 0; k < 3; k++) {
if (PixelColor[k] + B > 255) {
PixelColor[k] = 255;
}
else {
PixelColor[k] += B;
}
}
dst.at<Vec3b>(i, j) = PixelColor;
}
}
}

int main()
{
// reading source file srcImage
srcImage = imread("Samples/Fish.jpg");
if (!srcImage.data)
{
cout << "Error! Cannot read source file. Press ENTER.";
waitKey(); // wait for a key press
return(-1);
}
ShowImage(srcImage, "Aliaksandr Shydlouski", 0, 0);

cvtColor(srcImage, greyImage, COLOR_BGR2GRAY);


ShowImage(greyImage, "Gray Image", 300, 0);
imwrite("Samples/Grey Image.jpg", greyImage);

Mat resizedImage(200, 200, srcImage.type());


resize(srcImage, resizedImage, resizedImage.size());
ShowImage(resizedImage, "Resized Image", 600, 0);

Mat blurImage;
blur(srcImage, blurImage, Size(5, 5));
ShowImage(blurImage, "Blur Image", 900, 0);

// move the window to the specified position


Mat CannyImage;
Canny(srcImage, CannyImage, 200, 100);
ShowImage(CannyImage, "Canny Image", 1200, 0);

Mat LaplacianImage;
Laplacian(srcImage, LaplacianImage, CV_16S, 3);
Mat scaledLaplacianImage;
convertScaleAbs(LaplacianImage, scaledLaplacianImage);
ShowImage(scaledLaplacianImage, "Laplacian Image", 0, 300);
// rest of the code

Mat dilatedImage;
int iterations = 2;
Mat element = getStructuringElement(MORPH_RECT, Size(iterations * 2 + 1,
iterations * 2 + 1), Point(iterations, iterations));
dilate(CannyImage, dilatedImage, element);
ShowImage(dilatedImage, "Dilated Image", 300, 300);

Mat erodedImage;
erode(dilatedImage, erodedImage, element);
ShowImage(erodedImage, "Eroded Image", 600, 300);
//SHOW SHOW SHOW

Mat brightImage;
srcImage.copyTo(brightImage);
Brightness(srcImage, brightImage, 100);
ShowImage(brightImage, "Bright Image", 900, 300);

//SHOW SHO SHOW

CreateWindow("Binarization", 0, 600);
createTrackbar("Threshold", "Binarization", &threshold_value, max_value,
Threshold);
Threshold(threshold_value, 0);

//show show show


/*
waitKey(0); // wait for a key press
// CreateWindow("Src Video", 600, 600);
//CreateWindow("Dst Video", 1200, 600);
Mat srcFrame, dstFrame;
VideoCapture capture("samples/Dino.avi");
capture >> srcFrame;
VideoWriter writer("samples/Dino2.avi", -1, 25, srcFrame.size());
int key;
while (!srcFrame.empty())
{
key = waitKey(30);
if (key == 27) {
break;
}
if (!srcFrame.empty()) {
blur(srcFrame, dstFrame, Size(10,5));
writer << dstFrame;
imshow("Src video", srcFrame);
imshow("Dst video", dstFrame);
}
capture >> srcFrame;
}
//sho show show

*/

Mat histogram;
calcHist(&greyImage, 1, 0, Mat(), histogram, 1, &histSize, histRange);
Mat histImage(hist_h, hist_w, CV_8UC3, Scalar(0, 0, 0));
normalize(histogram, histogram, 0, histImage.rows, NORM_MINMAX, -1, Mat());
for (int i = 1; i < histSize; i++)
{
int value = cvRound(histogram.at<float>(i));
line(histImage, Point(i, hist_h), Point(i, hist_h - value), Scalar(255,
255, 255), 1);
}
ShowImage(histImage, "Gray image histogram", 300, 300);

Mat equalizedImage;
equalizeHist(greyImage, equalizedImage);
ShowImage(equalizedImage, "Equalized image histogram Image", 900, 300);

Mat equalizedHistogram;
calcHist(&equalizedImage, 1, 0, Mat(), equalizedHistogram, 1, &histSize,
histRange);
Mat equalizedHistImage(hist_h, hist_w, CV_8UC3, Scalar(0, 0, 0));
normalize(equalizedHistogram, equalizedHistogram, 0, equalizedHistImage.rows,
NORM_MINMAX, -1, Mat());
for (int i = 1; i < histSize; i++)
{
int value = cvRound(equalizedHistogram.at<float>(i));
line(equalizedHistImage, Point(i, hist_h), Point(i, hist_h - value),
Scalar(255, 255, 255), 1);
}
ShowImage(equalizedHistImage, "Histogram Equalized", 900, 300);

waitKey(0);
}

You might also like