OpenCV: Why is the ROI Mat distorted? - c++

Im having an issue where the ROI of an mat get distorted. I think the image below describes the problem best. The image is first turned gray with cv:cvtColor, then i use cv::threshold on the gray mat, then i use Canny on the threshold mat, then i find the contours. I go through all the contours and find the biggest contour based on area of the contour. I then use drawContour to use it as a mask on the threshold mat. And thats where i use the following code:
cv::Mat inMat = [self cvMatFromUIImage: inputImage];
cv::Mat grayMat, threshMat, canny_output;
cv::cvtColor(inMat, grayMat, CV_BGR2GRAY);
cv::threshold(grayMat, threshMat, 128, 255, CV_THRESH_BINARY | CV_THRESH_OTSU);
Canny(threshMat, canny_output, 100, 100, 3);
cv::Mat drawing = cv::Mat::zeros( canny_output.size(), CV_8UC3 );
std::vector<cv::Vec4i> hierarchy;
std::vector<std::vector<cv::Point> > contours;
cv::findContours(canny_output, contours, hierarchy, CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE);
NSMutableArray *contourAreasArray = [NSMutableArray new];
// Sorts the contour areas from greatest or smallest
for (int i = 0; i < contours.size(); ++i) {
NSNumber *contourArea = [NSNumber numberWithDouble:cv::contourArea(contours[i])];
[contourAreasArray addObject:contourArea];
}
NSSortDescriptor *highestToLowest = [NSSortDescriptor sortDescriptorWithKey:#"self" ascending:NO];
[contourAreasArray sortUsingDescriptors:[NSArray arrayWithObject:highestToLowest]];
cv::Rect maskRect;
Mat mask;
for (int i = 0; i < contours.size(); ++i)
{
NSNumber *contourArea = [NSNumber numberWithDouble:cv::contourArea(contours[i])];
cv::Rect brect = cv::boundingRect(contours[i]);
if ([contourArea doubleValue] == [[contourAreasArray objectAtIndex:0] doubleValue]) {
//NSLog(#"Drawing Contour");
maskRect = cv::boundingRect(contours[i]);
cv::drawContours(drawing, contours, i, cvScalar(255,255,255), CV_FILLED);
cv::rectangle(drawing, brect, cvScalar(255));
}
}
cv::Rect region_of_interest = maskRect;
cv::Mat roiImage = cv::Mat(threshMat, region_of_interest);
// final result is converted to UIImage for display
UIImage *threshUIImage = [self UIImageFromCVMat:drawing];
UIImage *resultUIImage = [self UIImageFromCVMat:roiImage];
self.plateImageView.image = resultUIImage;
self.threshImageView.image = threshUIImage;
Where am i going wrong here? If you guys need more code let me know, ill be more than happy to post more code... (the left image is the extracted (ROI Mat) and the right one is the mask that i obtain from drawContour).
And this is the picture I'm trying to process to get the license plate.

Alright well i finally figured it out...the solution was to do the following, essentially just coping the ROI image into another Mat:
cv::Rect region_of_interest = maskRect;
cv::Mat roiImage = cv::Mat(threshMat, region_of_interest);
cv::Mat finalROIImage;
roiImage.copyTo(finalROIImage);

Related

OpenCV C++ set background to transparent and crop

I am creating a stitching program and it works well. My next task is to crop the image so the image doesn't get too large and only contains the stitched areas. I am able to crop the image using the threshold() and findContours() functions. I then parse through the contours to determine where to crop. Another issue I wish to tackle in this function is to set the background to be transparent or alpha(255). My logic is take the base image to crop and do this step in conjunction with finding the area to crop. My code follows with portions that were taken from these two resources:
http://answers.opencv.org/question/37441/how-to-set-transparent-background-to-grabcut-output-image-in-opencv-c/
http://answers.opencv.org/question/24463/how-to-remove-black-background-from-grabcut-output-image-in-opencv-android/#24508
Mat crop(Mat image){
Mat cropped, grayed, thresh, transparent, result;
vector<vector<Point>> contours;
vector<Vec4i> hierarchy;
int largest_contour_index=0;
int largest_area=0;
Mat alpha(image.size(),CV_8UC1,Scalar(0));
cvtColor(image, grayed, CV_BGR2GRAY);
threshold( grayed, thresh, 1, 255,THRESH_BINARY);
findContours( thresh, contours, hierarchy, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE);
vector<int> x,y;
for(int i=0; i < contours.size(); i++){
for(int j = 0; j < contours.at(i).size();j++){
double a=contourArea( contours[i],false); // Find the area of contour
if(a>largest_area){
largest_area=a;
largest_contour_index=i; //Store the index of largest contour
}
x.push_back(contours[i][j].x);
y.push_back(contours[i][j].y);
}
}
drawContours( alpha,contours, largest_contour_index, Scalar(255),CV_FILLED, 8, hierarchy );
Mat rgb[3];
split(image,rgb);
Mat rgba[4]={rgb[0],rgb[1],rgb[2],alpha};
merge(rgba,4,transparent);
imshow("Transparent",transparent);
waitKey();
destroyAllWindows();
auto xVals = std::minmax_element(x.begin(), x.end());
auto yVals = std::minmax_element(y.begin(), y.end());
Rect rect (*xVals.first,*yVals.first,(*xVals.second)-(*xVals.first),(*yVals.second)-(*yVals.first));
cropped = image(rect);
return cropped;
}
The following image is the result of this code. As you can see the crop work but the image background is not transparent
Any help is appreciated!

Drawing Rectangle around difference area

I have a question which i am unable to resolve. I am taking difference of two images using OpenCV. I am getting output in a seperate Mat. Difference method used is the AbsDiff method. Here is the code.
char imgName[15];
Mat img1 = imread(image_path1, COLOR_BGR2GRAY);
Mat img2 = imread(image_path2, COLOR_BGR2GRAY);
/*cvtColor(img1, img1, CV_BGR2GRAY);
cvtColor(img2, img2, CV_BGR2GRAY);*/
cv::Mat diffImage;
cv::absdiff(img2, img1, diffImage);
cv::Mat foregroundMask = cv::Mat::zeros(diffImage.rows, diffImage.cols, CV_8UC3);
float threshold = 30.0f;
float dist;
for(int j=0; j<diffImage.rows; ++j)
{
for(int i=0; i<diffImage.cols; ++i)
{
cv::Vec3b pix = diffImage.at<cv::Vec3b>(j,i);
dist = (pix[0]*pix[0] + pix[1]*pix[1] + pix[2]*pix[2]);
dist = sqrt(dist);
if(dist>threshold)
{
foregroundMask.at<unsigned char>(j,i) = 255;
}
}
}
sprintf(imgName,"D:/outputer/d.jpg");
imwrite(imgName, diffImage);
I want to bound the difference part in a rectangle. findContours is drawing too many contours. but i only need a particular portion. My diff image is
I want to draw a single rectangle around all the five dials.
Please point me to right direction.
Regards,
I would search for the highest value for i index giving a non black pixel; that's the right border.
The lowest non black i is the left border. Similar for j.
You can:
binarize the image with a threshold. Background will be 0.
Use findNonZero to retrieve all points that are not 0, i.e. all foreground points.
use boundingRect on the retrieved points.
Result:
Code:
#include <opencv2/opencv.hpp>
using namespace cv;
int main()
{
// Load image (grayscale)
Mat1b img = imread("path_to_image", IMREAD_GRAYSCALE);
// Binarize image
Mat1b bin = img > 70;
// Find non-black points
vector<Point> points;
findNonZero(bin, points);
// Get bounding rect
Rect box = boundingRect(points);
// Draw (in color)
Mat3b out;
cvtColor(img, out, COLOR_GRAY2BGR);
rectangle(out, box, Scalar(0,255,0), 3);
// Show
imshow("Result", out);
waitKey();
return 0;
}
Find contours, it will output a set of contours as std::vector<std::vector<cv::Point> let us call it contours:
std::vector<cv::Point> all_points;
size_t points_count{0};
for(const auto& contour:contours){
points_count+=contour.size();
all_points.reserve(all_points);
std::copy(contour.begin(), contour.end(),
std::back_inserter(all_points));
}
auto bounding_rectnagle=cv::boundingRect(all_points);

Finding difference in an image

I have image as follows:
I want to detect 5 dials for processing. Hough circles is detecting all other irrelevant circles. to solve this i created a plain image and generated absolute difference with this one. It gave this image:
I drew box around it and final image is:
My code is as follows:
Mat img1 = imread(image_path1, COLOR_BGR2GRAY);
Mat img2 = imread(image_path2, COLOR_BGR2GRAY);
cv::Mat diffImage;
cv::absdiff(img2, img1, diffImage);
cv::Mat foregroundMask = cv::Mat::zeros(diffImage.rows, diffImage.cols, CV_8UC3);
float threshold = 30.0f;
float dist;
for(int j=0; j<diffImage.rows; ++j)
{
for(int i=0; i<diffImage.cols; ++i)
{
cv::Vec3b pix = diffImage.at<cv::Vec3b>(j,i);
dist = (pix[0]*pix[0] + pix[1]*pix[1] + pix[2]*pix[2]);
dist = sqrt(dist);
if(dist>threshold)
{
foregroundMask.at<unsigned char>(j,i) = 255;
}
}
}
cvtColor(diffImage,diffImage,COLOR_BGR2GRAY);
Mat1b img = diffImage.clone();
// Binarize image
Mat1b bin = img > 70;
// Find non-black points
vector<Point> points;
findNonZero(bin, points);
// Get bounding rect
Rect box = boundingRect(points);
// Draw (in color)
rectangle(img1, box, Scalar(0,255,0), 3);
// Show
imshow("Result", img1);
Now the issue is i cant compare plain image with anyother iamge of different sizes. Any pointer to right direction will be very helpful.
Regards,
Saghir A. Khatr
Edit
My plain image is as follows
I want to create a standard sample plain image which can be used with any image to detect that portion...

how to detect number of contours in between two lines or specified region of image

I am using OpenCV C++ to find the contours in a video. I want to count the no of contours present in video in a a specified region or in between two lines drawn in a video. For example a stream of contours are moving in a video and I want to count them when they reach to a specific region in a video. I will decrease the count as they leave the specific region in the video. I know some basic stuffs to find contour, calculate the area etc. But I am not getting any programming tips to count the no of contours within specified region. Please help me with the related topics and some tips on programming. (I do not want to use cvBlob.h library)
Basically I am counting the number of cars entered in that region. If car is entered I will increment the count and if it leaves the region then I will decrease the count.
You can approximate the contour you found to a polygon or circle:
for( int i = 0; i < contours.size(); i++ )
{ approxPolyDP( Mat(contours[i]), contours_poly[i], 3, true );
boundRect[i] = boundingRect( Mat(contours_poly[i]) )
}
after that use the line equation y=a and compare the coordinates of the corners of rectangle or the center+radius of the circle to determine if the contour passed the line.
1.Use a part of your Mat image.(If your ROI is rectangle)
Mat src; // suppose this is your source frame
Mat src_of_interest = src(Rect(Point(x1, y1), Point(x2, y2)));
// Do the rest of finding contour..
vector<vector<Point>> contours;
vector<Vec4i> hierarchy;
findContours(src_of_interest.clone(), contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE);
//now you can do your job with contour vector.
int count = contours.size();
Mat dst = Mat::zeros(src.size(), CV_8UC3);
for(int i=0; i< count; i++)
{
drawContours(dst, contours, i, CV_RGB(255, 0, 0)); // draw contour in red
}
2. If your Region of Interest is not rectangle, try this approach:
vector<Point> contour_of_interest; // this contour is where you want to check
vector<vector<Point>> contours; // this is the contours you found
Mat dst = Mat::zeros(src.size(), CV_8U);
for(int i=0; i< count; i++)
{
drawContours(dst, contours, i, Scalar(255)); // set contour area to 255
}
Mat roi = Mat::zeros(src.size(), CV_8U);
vector<vector<Point>> coi_vector;
coi_vector.push_back(contour_of_interest);
drawContours(roi, coi_vector, 0, Scalar(255));
Mat and = dst & roi; // where 2 mats are both TRUE(not zero)
findContours(and.clone(), contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE);
//now you can do your job with contour vector.
int count = contours.size();
3. If you want to count contours between 4 points, try this approach:
vector<Point> contour_of_interest; // this contour is the area where you want to check
Point p1(x1, y1);
Point p2(x1, y2);
Point p3(x2, y2);
Point p4(x2, y1);
contour_of_interest.push_back(p1);
contour_of_interest.push_back(p2);
contour_of_interest.push_back(p3);
contour_of_interest.push_back(p4);
vector<vector<Point>> coi_list;
coi_list.push_back(contour_of_interest);
Mat mask = Mat:zeros(src.size(), CV_8U);
drawContours(mask, coi_list, 0, Scalar(255));
Mat src; // suppose this is your source frame
Mat src_of_interest = src & mask; // remove any pixels outside mask area
// Do the rest of finding contour..
vector<vector<Point>> contours;
vector<Vec4i> hierarchy;
findContours(src_of_interest.clone(), contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE);
//now you can do your job with contour vector.
int count = contours.size();
Mat dst = Mat::zeros(src.size(), CV_8UC3);
for(int i=0; i< count; i++)
{
drawContours(dst, contours, i, CV_RGB(255, 0, 0)); // draw contour in red
}

Finding Contours in OpenCV?

When you retrieve contours from an image, you should get 2 contours per blob - one inner and one outer. Consider the circle below - since the circle is a line with a pixel width larger than one, you should be able to find two contours in the image - one from the inner part of the circle and one from the outer part.
Using OpenCV, I want to retrieve the INNER contours. However, when I use findContours (), I only seem to be getting the outer contours. How would I retrieve the inner contours of a blob using OpenCV?
I am using the C++ API, not C therefore only suggest functions that use the C++ API. (i.e. findContours () rather than cvFindContours ())
Thanks.
I ran this code on your image and it returned an inner and outer contour.
#include <iostream>
#include "opencv2/core/core.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
int main(int argc, const char * argv[]) {
cv::Mat image= cv::imread("../../so8449378.jpg");
if (!image.data) {
std::cout << "Image file not found\n";
return 1;
}
//Prepare the image for findContours
cv::cvtColor(image, image, CV_BGR2GRAY);
cv::threshold(image, image, 128, 255, CV_THRESH_BINARY);
//Find the contours. Use the contourOutput Mat so the original image doesn't get overwritten
std::vector<std::vector<cv::Point> > contours;
cv::Mat contourOutput = image.clone();
cv::findContours( contourOutput, contours, CV_RETR_LIST, CV_CHAIN_APPROX_NONE );
//Draw the contours
cv::Mat contourImage(image.size(), CV_8UC3, cv::Scalar(0,0,0));
cv::Scalar colors[3];
colors[0] = cv::Scalar(255, 0, 0);
colors[1] = cv::Scalar(0, 255, 0);
colors[2] = cv::Scalar(0, 0, 255);
for (size_t idx = 0; idx < contours.size(); idx++) {
cv::drawContours(contourImage, contours, idx, colors[idx % 3]);
}
cv::imshow("Input Image", image);
cvMoveWindow("Input Image", 0, 0);
cv::imshow("Contours", contourImage);
cvMoveWindow("Contours", 200, 0);
cv::waitKey(0);
return 0;
}
Here are the contours it found:
I think what Farhad is asking is to crop the contour from the original image.
To do that you will need to find the contour as explained above, then use a mask to get the inside from the original, and then crop the result into an image with the same size as the contour.
The findcontours function stores all the contours in different vectors, in the code given all contours are drawn, you just have draw the contour corresponding to the inner one, idx is the variable that states which contour is drawn.