Counting bright pixels and summing them. Medical Image C ++

I am currently working on a project in the field of medical technology. I have a large image with several sub-images of the cell, so my first task is to split the image.

I thought of the following:

Convert Image to Binary

projectes the brightness pixels onto the x axis, so I can see where there are gaps between the brightness values, and then split the image.

The problem arises when I try to reach the second part. My idea uses the vector as a projection and sums all the brightness values โ€‹โ€‹in one column, so the position number 0 of the vector is the sum of all the brightness values โ€‹โ€‹that are in the first column of the image, the same until I get to the last column, therefore at the end I have a projection.

Here is how I tried:

void calculo(cv::Mat &result,cv::Mat &binary){ //result=the sum,binary the imag. int i,j; for (i=0;i<=binary.rows;i++){ for(j=0;j<=binary.cols;j++){ cv::Scalar intensity= binaria.at<uchar>(j,i); result.at<uchar>(i,i)=result.at<uchar>(i,i)+intensity.val[0]; } cv::Scalar intensity2= result.at<uchar>(i,i); cout<< "content" "\n"<< intensity2.val[0] << endl; } } 

While executing this code, I have a violation error. Another problem is that I cannot create a matrix with one unique row, so ... I do not know what I could do.

Any ideas ?! Thanks!


In the end, this will not work, I need to sum all the pixels in one COLUMN. I did:

 cv::Mat suma(cv::Mat& matrix){ int i; cv::Mat output(1,matrix.cols,CV_64F); for (i=0;i<=matrix.cols;i++){ output.at<double>(0,i)=norm(matrix.col(i),1); } return output; } 

but this gave me an error: Assertion failed (0 <= colRange.start & colRange.start <= colRange.end & colRange.end <= m.cols) in Mat, file / home / usuario / OpenCV-2.2. 0 / modules / core / src / matrix.cpp, line 276

I donโ€™t know, any idea would be useful, anyway, thanks mevatron, you really left me on the way.

+1
source share
1 answer

If you just need the sum of the binary image, you can just take the L1 norm. For instance:

 Mat binaryVectorSum(const Mat& binary) { Mat output(1, binary.rows, CV_64F); for(int i = 0; i < binary.rows; i++) { output.at<double>(0, i) = norm(binary.row(i), NORM_L1); } return output; } 

I am at work, so I canโ€™t check it, but it should close you.

EDIT:. Tested. It works. :) One caveat ... this function works if your binary matrix is โ€‹โ€‹really binary (i.e. 0 and 1). You may need to scale the output signal with the maximum value if the binary matrix has a value of 0 and 255.

EDIT: If you do not have using namespace cv; in your .cpp file, you need to declare a namespace to use NORM_L1 , like this cv::NORM_L1 .

Have you considered matrix transfer before function call? Like this:

 sumCols = binaryVectorSum(binary.t()); 

vs.

 sumRows = binaryVectorSum(binary); 

EDIT: Error with my code :) I changed:

 Mat output(1, binary.cols, CV_64F); 

to

 Mat output(1, binary.rows, CV_64F); 

My test case was a square matrix, so no error was found ...

Hope this will be helpful!

+2
source

All Articles