Follow these steps to set up OpenCV in your VS2019 environment:
- Navigate to View → Other Windows → Property Manager → Add
Microsoft.Cpp.x64.userunderRelease|x64 - Configure include directories:
- Open properties of
Microsoft.Cpp.x64.user→ VC++ Directories → Include Directories - Add these paths:
D:\Libraries\opencv\build\include``D:\Libraries\opencv\build\include\opencv2
- Open properties of
- Configure library directories:
- In VC++ Directories → Library Directories
- Add:
D:\Libraries\opencv\build\x64\vc16\lib
- Configure linker:
- Linker → Input → Additoinal Dependencies
- Add:
opencv_world480.lib
- Set environment variables and restart VS:
- Add to system PATH:
D:\Libraries\opencv\build\x64\vc16\lib
- Add to system PATH:
Essential OpenCV Image Operations
1. Loading Images
The imread function reads images from disk:
// Parameters: file path and read mode (default: color)
Mat readImage(const String& filePath, int readMode = IMREAD_COLOR);
// Example usage
Mat sourceImage = readImage("C:/Images/sample.jpg", IMREAD_COLOR);
2. Saving Images
Use imwrite to save Mat objects to disk:
// Parameters: output path, Mat object, optional parameters
bool saveImage(const String& outputPath, InputArray imageData);
// Example
bool success = saveImage("C:/Output/result.png", processedImage);
3. Displaying Images
The imshow function creates windows to display images:
// Parameters: window name and Mat object
void showImage(const String& windowTitle, InputArray matrix);
// Example
showImage("Original Image", originalMatrix);
4. Color Space Conversion
Convert between different color formats using cvtColor:
// Parameters: source, destination, conversion code, channel count
void convertColorSpace(InputArray input, OutputArray output, int conversionCode);
// Examples
Mat hsvImage, grayImage;
convertColorSpace(sourceMatrix, hsvImage, COLOR_BGR2HSV); // BGR to HSV
convertColorSpace(sourceMatrix, grayImage, COLOR_BGR2GRAY); // BGR to Grayscale
5. Creating and Copying Mat Objects
// Create blank canvas
Mat blankCanvas = Mat::zeros(Size(640, 480), CV_8UC3);
// Deep copy using clone
Mat deepCopy1 = sourceMatrix.clone();
// Deep copy using copyTo
Mat deepCopy2;
sourceMatrix.copyTo(deepCopy2);
// Shallow copy (shares data)
Mat shallowCopy = sourceMatrix;
6. Accesssing Pixel Values
Two approaches for pixel manipulation:
Mat imageMatrix;
int width = imageMatrix.cols;
int height = imageMatrix.rows;
int channelCount = imageMatrix.channels();
// Method 1: Direct access (channel-specific)
for (int row = 0; row < height; ++row) {
for (int col = 0; col < width; ++col) {
if (channelCount == 1) {
// Grayscale image
uchar pixel = imageMatrix.at<uchar>(row, col);
imageMatrix.at<uchar>(row, col) = 255 - pixel;
} else if (channelCount == 3) {
// Color image
Vec3b colorPixel = imageMatrix.at<vec3b>(row, col);
imageMatrix.at<vec3b>(row, col)[0] = 255 - colorPixel[0];
imageMatrix.at<vec3b>(row, col)[1] = 255 - colorPixel[1];
imageMatrix.at<vec3b>(row, col)[2] = 255 - colorPixel[2];
}
}
}
// Method 2: Pointer access (recommended)
for (int row = 0; row < height; ++row) {
uchar* rowPointer = imageMatrix.ptr<uchar>(row);
for (int col = 0; col < width * channelCount; ++col) {
*rowPointer++ = 255 - *rowPointer;
}
}</uchar></vec3b></vec3b></vec3b></vec3b></uchar></uchar>
7. Pixel Arithmetic Operations
Perform mathematical operations on image pixels:
// Method 1: Matrix-level operations
Mat additionMatrix = Mat::zeros(sourceMatrix.size(), sourceMatrix.type());
additionMatrix = Scalar(50, 50, 50);
Mat resultAdd, resultSub, resultMul, resultDiv;
add(sourceMatrix, additionMatrix, resultAdd);
subtract(sourceMatrix, additionMatrix, resultSub);
multiply(sourceMatrix, Scalar(1.5, 1.5, 1.5), resultMul);
divide(sourceMatrix, Scalar(2.0, 2.0, 2.0), resultDiv);
// Method 2: Pixel-level operations with saturation
for (int row = 0; row < height; ++row) {
uchar* srcRow = sourceMatrix.ptr<uchar>(row);
uchar* dstRow = resultMatrix.ptr<uchar>(row);
for (int col = 0; col < width * channelCount; ++col) {
// Ensure values stay within 0-255 range
*dstRow++ = saturate_cast<uchar>(*srcRow * 1.2 + 30);
srcRow++;
}
}</uchar></uchar></uchar>
8. Creating Interactive Controls
Implement real-time adjustments using trackbars:
// Trackbar callback function prototype
void onTrackbarChange(int value, void* userData);
// Create trackbar for brightness control
createTrackbar("Brightness", "Control Window",
&brightnessValue, 100, onTrackbarChange, &imageMatrix);