-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimage_processing.cpp
More file actions
188 lines (158 loc) · 7.82 KB
/
image_processing.cpp
File metadata and controls
188 lines (158 loc) · 7.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#include "image_processing.hpp"
#include <iostream>
#include <string>
#include <opencv2/opencv.hpp>
#include <algorithm> // for std::transform
#include <cctype> // for std::toupper
// Convert the frame to grayscale and display it
void showGrayscale(const cv::Mat& frame) {
cv::Mat grayscaleImage;
cv::cvtColor(frame, grayscaleImage, cv::COLOR_BGR2GRAY); // Convert the input frame to grayscale
cv::imshow("Grayscale Image", grayscaleImage); // Display the grayscale image
}
// Convert the frame to HSV and display it
void showHSV(const cv::Mat& frame) {
cv::Mat hsvImage;
cv::cvtColor(frame, hsvImage, cv::COLOR_BGR2HSV); // Convert the input frame to HSV color space
cv::imshow("HSV Image", hsvImage); // Display the HSV image
}
// Apply Gaussian blur to the image and display it
void showBlurred(const cv::Mat& frame, int kernelSize) {
// Ensure the kernel size is valid
if (kernelSize <= 0 || kernelSize % 2 == 0) {
std::cout << "Invalid kernel size. Using default kernel size of 15." << std::endl;
kernelSize = 15; // Set default kernel size if input is invalid
}
cv::Mat blurredImage;
cv::GaussianBlur(frame, blurredImage, cv::Size(kernelSize, kernelSize), 0); // Apply Gaussian blur
cv::imshow("Blurred Image", blurredImage); // Display the blurred image
}
// Apply thresholding to the grayscale image and display the result
void showThresholded(const cv::Mat& frame, int thresholdValue) {
// Ensure the threshold value is valid
if (thresholdValue < 0 || thresholdValue > 255) {
std::cout << "Invalid threshold value. Using default threshold of 128." << std::endl;
thresholdValue = 128; // Use default threshold if input is invalid
}
cv::Mat grayscaleImage, thresholdedImage;
cv::cvtColor(frame, grayscaleImage, cv::COLOR_BGR2GRAY); // Convert to grayscale
cv::threshold(grayscaleImage, thresholdedImage, thresholdValue, 255, cv::THRESH_BINARY); // Apply binary thresholding
cv::imshow("Thresholded Image", thresholdedImage); // Display the thresholded image
}
// Crop the image and display it
void showCropped(const cv::Mat& frame, int x, int y, int width, int height) {
// Ensure the cropping dimensions are valid
if (x >= 0 && y >= 0 && x + width <= frame.cols && y + height <= frame.rows) {
cv::Mat croppedImage = frame(cv::Rect(x, y, width, height)); // Crop the image
cv::imshow("Cropped Image", croppedImage); // Display the cropped image
} else {
std::cout << "Invalid cropping dimensions!" << std::endl;
}
}
// Resize the image and display it
void showResized(const cv::Mat& frame, int newWidth, int newHeight) {
// Ensure valid resizing dimensions
if (newWidth > 0 && newHeight > 0) {
cv::Mat resizedImage;
cv::resize(frame, resizedImage, cv::Size(newWidth, newHeight)); // Resize the image
cv::imshow("Resized Image", resizedImage); // Display the resized image
} else {
std::cout << "Invalid dimensions for resizing!" << std::endl;
}
}
// Rotate the image and display it
void showRotated(const cv::Mat& frame, double angle) {
// Define the center of the image for rotation
cv::Point2f center(frame.cols / 2.0f, frame.rows / 2.0f);
cv::Mat rotationMatrix = cv::getRotationMatrix2D(center, angle, 1.0); // Generate rotation matrix
cv::Mat rotatedImage;
cv::warpAffine(frame, rotatedImage, rotationMatrix, frame.size()); // Rotate the image
cv::imshow("Rotated Image", rotatedImage); // Display the rotated image
}
// Apply convolution to the image and display the result
void applyConvolution(const cv::Mat& frame, const cv::Mat& kernel) {
// Validate the kernel size
if (kernel.empty() || kernel.rows % 2 == 0 || kernel.cols % 2 == 0) {
std::cout << "Invalid kernel. Please provide a valid odd-sized kernel." << std::endl;
return;
}
cv::Mat result;
cv::filter2D(frame, result, -1, kernel); // Perform 2D convolution
cv::imshow("Convolution Result", result); // Display the result of the convolution
}
// Apply erosion to the image and display the result
void applyErosion(const cv::Mat& frame, int kernelSize) {
// Validate the kernel size
if (kernelSize <= 0 || kernelSize % 2 == 0) {
std::cout << "Invalid kernel size. Using default size of 3." << std::endl;
kernelSize = 3; // Use default kernel size if input is invalid
}
// Define the kernel for erosion
cv::Mat element = cv::getStructuringElement(cv::MORPH_RECT, cv::Size(kernelSize, kernelSize));
cv::Mat erodedImage;
cv::erode(frame, erodedImage, element); // Apply erosion
cv::imshow("Eroded Image", erodedImage); // Display the eroded image
}
// Apply dilation to the image and display the result
void applyDilation(const cv::Mat& frame, int kernelSize) {
// Validate the kernel size
if (kernelSize <= 0 || kernelSize % 2 == 0) {
std::cout << "Invalid kernel size. Using default size of 3." << std::endl;
kernelSize = 3; // Use default kernel size if input is invalid
}
// Define the kernel for dilation
cv::Mat element = cv::getStructuringElement(cv::MORPH_RECT, cv::Size(kernelSize, kernelSize));
cv::Mat dilatedImage;
cv::dilate(frame, dilatedImage, element); // Apply dilation
cv::imshow("Dilated Image", dilatedImage); // Display the dilated image
}
// Perform Canny edge detection and display the result
void applyCanny(const cv::Mat& frame, int lowerThreshold, int upperThreshold) {
cv::Mat edges, imgGray;
cv::cvtColor(frame, imgGray, cv::COLOR_BGR2GRAY);
cv::Canny(imgGray, edges, lowerThreshold, upperThreshold); // Apply Canny edge detection
cv::imshow("Canny Edge Detection", edges); // Display edge-detected image
}
// void detectColor(const cv::Mat& frame, const cv::Scalar& lowerBound, const cv::Scalar& upperBound) {
// cv::Mat mask, result;
// cv::inRange(frame, lowerBound, upperBound, mask); // Mask the image based on the color range
// cv::bitwise_and(frame, frame, result, mask); // Apply the mask to the original frame
// cv::imshow("Detected Color", result); // Display the result
// }
// void onTrackbarChange(int, void* userdata) {
// // Callback function does nothing; it’s here to satisfy OpenCV’s API
// }
void detectColor(const cv::Mat &frame, std::string choice, int (&lowerBound)[3], int (&upperBound)[3]) {
// Create a window for color detection
const std::string windowName = "Color Detection";
cv::namedWindow(windowName, cv::WINDOW_AUTOSIZE);
// Extract current HSV bounds
int lowerH = static_cast<int>(lowerBound[0]);
int lowerS = static_cast<int>(lowerBound[1]);
int lowerV = static_cast<int>(lowerBound[2]);
int upperH = static_cast<int>(upperBound[0]);
int upperS = static_cast<int>(upperBound[1]);
int upperV = static_cast<int>(upperBound[2]);
// Update HSV bounds from trackbars
cv::Scalar lowerBoundn = cv::Scalar(lowerH, lowerS, lowerV);
cv::Scalar upperBoundn = cv::Scalar(upperH, upperS, upperV);
// Convert choice to uppercase
std::transform(choice.begin(), choice.end(), choice.begin(), ::toupper);
if (choice == "HSV") {
cv::Mat hsv;
cv::cvtColor(frame, hsv, cv::COLOR_BGR2HSV);
// Apply the color filter
cv::Mat mask;
cv::inRange(hsv, lowerBoundn, upperBoundn, mask);
// Combine the mask with the original frame
cv::Mat result;
cv::bitwise_and(frame, frame, result, mask);
// Display the result on same window
cv::imshow(windowName, result);
} else if (choice == "RBG" || choice == "BGR"){
cv::Mat mask, result;
cv::inRange(frame, lowerBoundn, upperBoundn, mask); // Mask the image based on the color range
cv::bitwise_and(frame, frame, result, mask); // Apply the mask to the original frame
cv::imshow(windowName, result); // Display the result
}
}