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

Open CV Lab

This document provides code snippets in C++ using OpenCV for image processing tasks like loading an image, displaying it, flipping it, detecting colors within a threshold range, counting pixels of a specific color, combining multiple color detections, modifying an image's color, changing color per row and column, and detecting colors in real-time from a video camera stream. Functions used include imread, imshow, flip, inRange, countNonZero, and VideoCapture. Output is displayed using namedWindow.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views4 pages

Open CV Lab

This document provides code snippets in C++ using OpenCV for image processing tasks like loading an image, displaying it, flipping it, detecting colors within a threshold range, counting pixels of a specific color, combining multiple color detections, modifying an image's color, changing color per row and column, and detecting colors in real-time from a video camera stream. Functions used include imread, imshow, flip, inRange, countNonZero, and VideoCapture. Output is displayed using namedWindow.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

#include <opencv2/highgui/highgui.

hpp>
#include <iostream>

using namespace cv;


using namespace std; //std is the standard namespace you can also call these functions
using std::cout

int main()

{
Mat image; //
image = imread("saba.png"); // Read the file

if (!image.data) // Check for invalid input


{
cout << "Could not open or find the image" << endl;
return -1;
}

namedWindow("Display window", WINDOW_AUTOSIZE);// Create a window for display.


imshow("Display window", image); // Show our image inside it.

waitKey(0); // Wait for a keystroke in


the window
return 0;
}

for flip
Mat result;
flip(input,result,-1);
namedwindow("output,"window_autosize);
imshow("output",ressult);

for detect colour


bgr (blue,green,red)
for blue
(100,0,0)-(255,100,100)
for green
(0,100,0)-(100,255,100)
for red
(0,0,100)-(100,100,255)

Mat output;
inRange(image, Scalar(100, 0, 0), Scalar(255, 100, 100), output);
namedWindow("Display",WINDOW_AUTOSIZE);
imshow("Display", output);
no of pixels
int x= countNonZero(output);
Cout<< Pixels;<<x;

For combine colour detection


Mat combine;
inRange(image, Scalar(0, 100, 0), Scalar(100, 255, 100), output); //green

inRange(image, Scalar(100, 0, 0), Scalar(255, 100, 100), output); //blue

inRange(image, Scalar(0, 0,100), Scalar(100, 100, 255), output); //red

combine = output | output1 | output2;

namedWindow("Display",WINDOW_AUTOSIZE);

imshow("Display", combine);

For changing the colour of image


for (int i = 0; i < image.rows; i++) {

for (int j = 0; j < image.cols; j++)

image.at<Vec3b>(i, j)[0] = 255;

image.at<Vec3b>(i, j)[1] = 0;

image.at<Vec3b>(i, j)[2] = 0;

namedWindow("Display", WINDOW_AUTOSIZE);

imshow("Display", image);

For changing colour of every row and col


cout << "SIZE " << image.rows << "x" << image.cols;

int b = 10, g = 50, r = 100;

for (int i = 0; i < image.rows; i++) {


for (int j = 0; j < image.cols; j++)

image.at<Vec3b>(i, j)[0] = b;

image.at<Vec3b>(i, j)[1] = g;

image.at<Vec3b>(i, j)[2] = r;

b = b+23;

g = g+90;

r = r+123;

namedWindow("Display", WINDOW_AUTOSIZE);

imshow("Display", image);

Open video camera and detect colour

VideoCapture cap(0);

while (1) {

Mat frame, output, output1, output2, combine;

bool bsucess = cap.read(frame);

namedWindow("Display", WINDOW_AUTOSIZE);

imshow("Display", frame);

inRange(frame, Scalar(100, 0, 0), Scalar(255, 100, 100), output); //blue

inRange(frame, Scalar(0, 100, 0), Scalar(100, 255, 100), output1);


//green

inRange(frame, Scalar(0, 0, 100), Scalar(100, 100, 255), output2); //red

combine = output | output1 | output2;

namedWindow("Display", WINDOW_AUTOSIZE);

imshow("Display", combine);
if (waitKey(30) == 27) {

cout << "esc key is pressed" << endl ;

break;

You might also like