Untitled

 avatar
unknown
plain_text
5 months ago
2.7 kB
3
Indexable
nclude <iostream>
#include <stdio.h>
#include "cv.h" //main opencv header
#include "highgui.h" //GUI header

using namespace std;

int main(int argc, char *argv[])
{
	IplImage* img;
	img = cvLoadImage(argv[1]);
	if (!img) {
	printf("Could not load image file: %s\n", argv[1]);
	return 1;
	}
	
	int ch = img->nChannels;
	int h = img->height;
	int w = img->width;
	int step = img->widthStep; //one width of data elements
	//uchar* data = (uchar *)img->imageData;
	//int b = data[0];
	//int g = data[1];
	//int r = data[2];

	//02. Print these details when picture is opened
	printf("Image Properties : \n");
	printf("No of chanels : %d\n", ch);
	printf("Height : %d\n", h);
	printf("Width : %d\n", w);
	printf("WidthStep : %d\n", step);

	//Iterate through each pixel in the 10*10 area
	int max_x = img->width < 10 ? img->width : 10;
	int max_y = img->height < 10 ? img->height : 10;

	IplImage* grey = cvCreateImage(cvSize(w, h), IPL_DEPTH_8U, 1);


	for (int y = 0; y < max_y; y++) {
		for (int x = 0; x < max_x; x++) {
			//calculate  the pointer to the pixel
			uchar* data = (uchar*)(img->imageData + y* img->widthStep + x* img->nChannels);
			//IplImage* grey = cvCreateImage(cvSize(w, h), IPL_DEPTH_8U, 1);
			uchar* data2 = (uchar*)(img->imageData);

			//Extracting B,G,R values
			int b = data[0];
			int g = data[1];
			int r = data[2];

			//point the pixel value 
			printf("Pixel (%d , %d): B =%d , G=%d ,R=%d\n", x, y, b, g, r);

			//changing 100x100 area black
			for (int i = 0; i < 100 && i < h; ++i) {
				for (int j = 0; j < 100 && j<w; ++j)
				{
					data[i * step + j * ch + 0] = 0;
					data[i * step + j * ch + 1] = 0;
					data[i * step + j * ch + 2] = 0;
				}
			}

			//changing 100x100 area white
			for (int i = 0; i < 100 && i < h; ++i) {
				for (int j = 0; j < 100 && j<w; ++j)
				{
					data[i * step + j * ch + 0] = 255;
					data[i * step + j * ch + 1] = 255;
					data[i * step + j * ch + 2] = 255;
				}
			}

			//GrayScale
			double blue;
			double green;
			double red;
			for (int i = 0; i < h; i++) {
				for (int j = 0; j < w ; j++) {
					blue = data[0];
					green = data[1];
					red = data[2];

					double grey_Val = 0.1140 * blue + 0.5870 * green + 0.2989 * red;
					data2[i*w + j * 1] = uchar(grey_Val);
				}
				
			}
		
		}
	}

	

	
	cvNamedWindow("Image");
	cvShowImage("Image", img);
	cvWaitKey(0);
	cvDestroyWindow("Image");
	cvReleaseImage(&img);

	cvNamedWindow("Image2");
	cvShowImage("Image2", grey);
	cvWaitKey(0);
	cvDestroyWindow("Image2");
	cvReleaseImage(&grey);
	
	return 0;
} 
Editor is loading...
Leave a Comment