Open CV Lab

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 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