n0bod1
n0bod1

Reputation: 365

Counting black pixels

I am using older version of C because the book I am using is outdated :( Currently, I am working on a project to detect an object in an image. First I do Gaussian smoothing on the gray scale image, then erode it. After that, I apply threshold. Now I am trying to obtain how many black pixels there are for every width so that I can compare it with other row to determine the center. I am trying this in 'for' loop, however, I am keep getting the error:

term does not evaluate to a function taking 1 arguments

#include <highgui.h>
#include <cv.h>
#include <cxcore.h>

int main()
{
    int total,  
        zero,
        width,
        blackpixel;

    IplImage* in = cvLoadImage("Wallet.jpg", CV_LOAD_IMAGE_GRAYSCALE);
    IplImage* gsmooth = cvCreateImage(cvGetSize(in), IPL_DEPTH_8U, 1);
    IplImage* erode = cvCreateImage(cvGetSize(in), IPL_DEPTH_8U, 1);
    IplImage* Iat = cvCreateImage(cvGetSize(in), IPL_DEPTH_8U, 1);
    IplImage* bpixel = cvCreateImage(cvGetSize(in), IPL_DEPTH_8U, 1);

    cvSmooth(in, gsmooth, CV_GAUSSIAN, 3, 0, 0, 0);
    cvErode(gsmooth, erode, NULL, 2);
    cvThreshold(erode, Iat, 100, 255, CV_THRESH_BINARY);

    total = (Iat->height)*(Iat->width);

    zero = total - cvCountNonZero(Iat);

    printf("Total pixels: %d\nWhite pixels: %d\nBlack pixels: %d\n", total, cvCountNonZero(Iat), zero);

    for(int i = 0; i < Iat->width; i++)
    {
        blackpixel = Iat->width(i);
    }

    cvNamedWindow("Original", 1);
    cvNamedWindow("Gaussian Smoothing", 1);
    cvNamedWindow("Erode", 1);
    cvNamedWindow("Adaptive Threshold", 1);

    cvShowImage("Original", in);
    cvShowImage("Gaussian Smoothing", gsmooth);
    cvShowImage("Erode", erode);
    cvShowImage("Adaptive Threshold", Iat);

    cvWaitKey(0);

    cvReleaseImage(&in);
    cvReleaseImage(&gsmooth);
    cvReleaseImage(&erode);
    cvReleaseImage(&Iat);

    cvDestroyWindow("Original");
    cvDestroyWindow("Gaussian Smoothing");
    cvDestroyWindow("Erode");
    cvDestroyWindow("Adaptive Threshold");
}

Upvotes: 0

Views: 1133

Answers (2)

morynicz
morynicz

Reputation: 2332

First of all, don't be afraid to use C++ API when using an outdated book like "Learining OpenCV", because the concepts are still relevant. Translating to C++ API is not hard if You understand the idea, and is a great exercise because You can't just copy-paste the code. I learned OpenCV this way, and I think it worked :).

With C++ API it would be as simple as

cv::Mat zeros = cv::Mat::zeros(Iat.size());
cv::Mat blackPixels = (Iat == zeros);
int blackPixelsCount = blackPixels.total();

Upvotes: 1

The problem in the line

blackpixel = Iat->width(i);

is the wrong syntax.

Iat->width will give you the width of the image, an integer property.

I don't thing that the loop

for(int i = 0; i < Iat->height; i++) 
    {
        blackpixel = Iat->width(i);
    }

can calculate the number of black pixels in a given row. You might need something like

for(int i = 0; i < Iat->height; i++) // // every row
{
    for(int j = 0; j < Iat->width; j++) // pixels in each row
    {
       // get count pixels here
    }
    // do things with the count for the current row
}

If you are using a cvMat data structure instead of IplImage, this should be faster.

Upvotes: 0

Related Questions