Click here to Skip to main content
15,867,453 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i'll create histogram projection (horizontal histogram and vertical histogram)from image that the image after i do process filter image and thresholding. how should i do???

for get the image i'm using :

Mat img  = cvLoadImage("C:/Chipmonk.jpg", CV_LOAD_IMAGE_COLOR); //open and read the image

	if (img.empty())
	{
		cout << "Image cannot be loaded..!!" << endl;
		return -1;
	}


then i do filter average and edge detection. then i do process threshold
after that i'll create histogram projection.

what should i do?

thanks before
Posted
Updated 7-Apr-14 22:49pm
v2
Comments
Rage 8-Apr-14 4:49am    
Can you post what you have tried so far, e.g. the steps:

"then i do filter average and edge detection. then i do process threshold
after that i'll create histogram projection."
dimas92 10-Apr-14 4:45am    
i do my program after process that like this.

Mat meanfilter;
cv::blur(img, meanfilter, cv::Size(3,3));

Mat src_gray;
Mat sobel;
int scale = 1;
int delta = 0;
int ddepth = CV_16S;

cvtColor(meanfilter, src_gray, CV_RGB2GRAY);

Mat sobelx, sobely;
Mat abs_sobelx, abs_sobely;

Sobel(src_gray, sobelx, ddepth, 1,0,3,scale,delta,BORDER_DEFAULT);
convertScaleAbs(sobelx,abs_sobelx);

Sobel(src_gray,sobely,ddepth,0,1,3,scale,delta,BORDER_DEFAULT);
convertScaleAbs(sobely,abs_sobely);

addWeighted(abs_sobelx,0.5,abs_sobely,0.5,0,sobel);

Mat Threshold, sobelThreshold;
cv::threshold(sobel,sobelThreshold,100,255,cv::THRESH_BINARY);



After that code, i'll do histogram projection

1 solution

I am not sure whether this is you want exactly. But have a look on the following code. It creates horizontal and vertical histograms of a thresholded image. What it actually does is simply count all nonzero pixels and store in a Mat object with respect to X or Y coordinate.

//To store the gray version of the image
	Mat gray;
	//To store the thresholded image
	Mat ret;
	//convert the image to grayscale
	cvtColor(img,gray,CV_BGR2GRAY);
	imshow("Gray Image",gray);
	//threshold the image
	threshold(gray,ret,0,255,CV_THRESH_BINARY_INV+CV_THRESH_OTSU);

	Mat horizontal(ret.cols,1,CV_32S);//horizontal histogram
	horizontal = Scalar::all(0);
	Mat vertical(ret.rows,1,CV_32S);//vertical histogram	
	vertical = Scalar::all(0);
	
	for(int i=0;i<ret.cols;i++)
	{
		horizontal.at<int>(i,0)=countNonZero(ret(Rect(i,0,1,ret.rows)));
	}

	for(int i=0;i<ret.rows;i++)
	{
		vertical.at<int>(i,0) = countNonZero(ret(Rect(0,i,ret.cols,1)));
	}
 
Share this answer
 
Comments
dimas92 12-Apr-14 14:41pm    
i'd do your solution but i still can't show this histogram to window. the window just black screen.
what should i do??

and then i'll do calculate the point peak of that histogram.
[no name] 12-Apr-14 23:29pm    
If you have done with the above code then you already have the two histograms. You can calculate the peak even without showing the histograms. I doubt you tried to display the histograms with imshow function. Since the histogram is in the dimension of 1 x image_width and 1 x image_height, you cannot see anything in the window. If you need to show the histogram then you need to normalize the histogram and draw the histogram lines manually in a Mat object, so then you can show it.
1. Use normalize function with NORM_MINMAX parameter to normalize the histogram.
2. Draw lines that shows the frequencies of the histogram by their heights.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900