Ask Your Question
0

Scan and find the length and breadth of object from iPhone camera

asked 2016-03-01 00:11:45 -0600

updated 2019-12-09 07:55:39 -0600

Akhil Patel gravatar image

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.image description

edit retag flag offensive close merge delete

Comments

So, I'm not quite sure what the question you're asking is. What image are you trying to separate the black and white pixels on? Did you already try to use the threshold function? That turns the image into a binary image with only black and white pixels.

Tetragramm gravatar imageTetragramm ( 2016-03-01 16:41:43 -0600 )edit

I'm not quite sure what the question you're asking is

There is hydraulic part that you can see in the above image, now we have to find the length, breadth and outside diameter of that hydraulic part.

What image are you trying to separate the black and white pixels on?

So, the approach is to convert above image into grayscale and separate the pixels of the object displayed in above image.

Did you already try to use the threshold function?

Regards to threshold function, I think I am using Harris Edge detection algorithm so it is already applied.

That turns the image into a binary image with only black and white pixels.

My concern is that if I used threshold function that you mention, my complete image turns into black color.

Sujit Baranwal gravatar imageSujit Baranwal ( 2016-03-01 22:51:40 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2016-03-02 18:56:46 -0600

Tetragramm gravatar image

I moved this to an answer so I can post pictures.

So, let's start with the first part of this problem, which is isolating the object.

You said you've tried Canny to get the edges. Did that not work? I'm getting a nice picture using your sample image there. Running findContours on it to show each separate edge as a different color gives this: image description

Some processing to close gaps and then some basic intelligence to limit it to inside the white areas and ignore the writing should work for just about everything.

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2016-03-01 00:11:45 -0600

Seen: 216 times

Last updated: Mar 02 '16