I want to find the length, breadth and outside diameter of the object
My analysis so far:
Applied the multiple algorithms like Canny and Sobel to detect the edges of the object.
I converted a image into Matrix Array:
//Function that convert image to matrix array. - (cv::Mat)cvMatFromUIImage:(UIImage *)image { CGColorSpaceRef colorSpace = CGImageGetColorSpace(image.CGImage); CGFloat cols = image.size.width; CGFloat rows = image.size.height;
cv::Mat cvMat(rows, cols, CV_8UC4); // 8 bits per component, 4 channels (color channels + alpha)
CGContextRef contextRef = CGBitmapContextCreate(cvMat.data, // Pointer to data
cols, // Width of bitmap
rows, // Height of bitmap
8, // Bits per component
cvMat.step[0], // Bytes per row
colorSpace, // Colorspace
kCGImageAlphaNoneSkipLast |
kCGBitmapByteOrderDefault); // Bitmap info flags
CGContextDrawImage(contextRef, CGRectMake(0, 0, cols, rows), image.CGImage);
CGContextRelease(contextRef);
return cvMat;
}
//Use function to create a matrix object.
cv::Mat cvImage = [self cvMatFromUIImage:image];
Then I apply the canny:
cv::Canny(grayImage, edges, m_cannyLoThreshold, m_cannyHiThreshold, m_cannyAperture * 2 + 1);
Same way I applied Sobel algorithm as well:
int scale = 1;
int delta = 0;
int ddepth = CV_16S;
/// Gradient X
cv::Sobel( grayImage, grad_x, ddepth, 1, 0, 3, scale, delta, cv::BORDER_DEFAULT );
cv::convertScaleAbs( grad_x, abs_grad_x );
/// Gradient Y
cv::Sobel( grayImage, grad_y, ddepth, 0, 1, 3, scale, delta, cv::BORDER_DEFAULT );
cv::convertScaleAbs( grad_y, abs_grad_y );
/// Total Gradient (approximate)
cv::addWeighted( abs_grad_x, 0.5, abs_grad_y, 0.5, 0, edges );
Now, I am separating the black and white pixels on the image. But every pixels that is looking black on device return some other pixel color.
What I have to do, separating the pixels from the image so that I get the actual height and width of object in image. Any suggestion ?
I have to detect the object in below image.