Ask Your Question

# Revision history [back]

### Detecting rectangular part of the image

I am trying to find the rectangular regions from the image , but i am not getting any good result

Below the code for finding square :

void find_squares(Mat& image, vector<vector<Point> >& squares)
{
// blur will enhance edge detection
Mat blurred(image);
medianBlur(image, blurred, 5);

Mat gray0(blurred.size(), CV_8U), gray;
vector<vector<Point> > contours;

// find squares in every color plane of the image
for (int c = 0; c < 1; c++)
{
int ch[] = {c, 0};
mixChannels(&blurred, 1, &gray0, 1, ch, 1);

// try several threshold levels
const int threshold_level = 2;
for (int l = 0; l < threshold_level; l++)
{
// Use Canny instead of zero threshold level!
// Canny helps to catch squares with gradient shading
if (l == 0)
{
Canny(gray0, gray, 10, 20, 3); //

// Dilate helps to remove potential holes between edge segments
dilate(gray, gray, Mat(), Point(-1,-1));
}
else
{
gray = gray0 >= (l+1) * 255 / threshold_level;
}

// Find contours and store them in a list
findContours(gray, contours, CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE);

// Test contours
vector<Point> approx;
for (size_t i = 0; i < contours.size(); i++)
{
// approximate contour with accuracy proportional
// to the contour perimeter
approxPolyDP(Mat(contours[i]), approx, arcLength(Mat(contours[i]), true)*0.02, true);

// Note: absolute value of an area is used because
// area may be positive or negative - in accordance with the
// contour orientation
if (approx.size() == 4 &&
fabs(contourArea(Mat(approx))) > 1000 &&
isContourConvex(Mat(approx)))
{
double maxCosine = 0;

for (int j = 2; j < 5; j++)
{
double cosine = fabs(angle(approx[j%4], approx[j-2], approx[j-1]));
maxCosine = MAX(maxCosine, cosine);
}

if (maxCosine < 0.3)
squares.push_back(approx);
}
}
}
}
}


and here is what i am doing in main :

vector<vector<Point> > squares;
find_squares(img, squares);
std::cout << "squares: " << squares.size() << std::endl;

draw_Squares(squares , img);

imwrite("area.png", img);
}


input image : required output image : ### Detecting rectangular part of the image

I am trying to find the rectangular regions from the image , but i am not getting any good result and also getting results slow , due to visiting every channel

Below the code for finding square :

void find_squares(Mat& image, vector<vector<Point> >& squares)
{
// blur will enhance edge detection
Mat blurred(image);
medianBlur(image, blurred, 5);

Mat gray0(blurred.size(), CV_8U), gray;
vector<vector<Point> > contours;

// find squares in every color plane of the image
//also tried one by one
for (int c = 0; c < 1; c++)
{
int ch[] = {c, 0};
mixChannels(&blurred, 1, &gray0, 1, ch, 1);

// try tried several threshold levels
const int threshold_level = 2;
for (int l = 0; l < threshold_level; l++)
{
// Use Canny instead of zero threshold level!
// Canny helps to catch squares with gradient shading
if (l == 0)
{
Canny(gray0, gray, 10, 20, 3); //

// Dilate helps to remove potential holes between edge segments
dilate(gray, gray, Mat(), Point(-1,-1));
}
else
{
gray = gray0 >= (l+1) * 255 / threshold_level;
}

// Find contours and store them in a list
findContours(gray, contours, CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE);

// Test contours
vector<Point> approx;
for (size_t i = 0; i < contours.size(); i++)
{
// approximate contour with accuracy proportional
// to the contour perimeter
approxPolyDP(Mat(contours[i]), approx, arcLength(Mat(contours[i]), true)*0.02, true);

// Note: absolute value of an area is used because
// area may be positive or negative - in accordance with the
// contour orientation
if (approx.size() == 4 &&
fabs(contourArea(Mat(approx))) > 1000 &&
isContourConvex(Mat(approx)))
{
double maxCosine = 0;

for (int j = 2; j < 5; j++)
{
double cosine = fabs(angle(approx[j%4], approx[j-2], approx[j-1]));
maxCosine = MAX(maxCosine, cosine);
}

if (maxCosine < 0.3)
squares.push_back(approx);
}
}
}
}
}


and here is what i am doing in main :

vector<vector<Point> > squares;
find_squares(img, squares);
std::cout << "squares: " << squares.size() << std::endl;

draw_Squares(squares , img);

imwrite("area.png", img);
}


input image :  required output image : ### Detecting rectangular part of the image

I am trying to find the rectangular regions from the image , but i am not getting any good result and also getting results slow , due to visiting every channel

Below the code for finding square :

void find_squares(Mat& image, vector<vector<Point> >& squares)
{
// blur will enhance edge detection
Mat blurred(image);
medianBlur(image, blurred, 5);

Mat gray0(blurred.size(), CV_8U), gray;
vector<vector<Point> > contours;

// find squares in every color plane of the image
//also tried one by one
for (int c = 0; c < 1; c++)
{
int ch[] = {c, 0};
mixChannels(&blurred, 1, &gray0, 1, ch, 1);

// tried several threshold levels
const int threshold_level = 2;
for (int l = 0; l < threshold_level; l++)
{
// Use Canny instead of zero threshold level!
// Canny helps to catch squares with gradient shading
if (l == 0)
{
Canny(gray0, gray, 10, 20, 3); //

// Dilate helps to remove potential holes between edge segments
dilate(gray, gray, Mat(), Point(-1,-1));
}
else
{
gray = gray0 >= (l+1) * 255 / threshold_level;
}

// Find contours and store them in a list
findContours(gray, contours, CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE);

// Test contours
vector<Point> approx;
for (size_t i = 0; i < contours.size(); i++)
{
// approximate contour with accuracy proportional
// to the contour perimeter
approxPolyDP(Mat(contours[i]), approx, arcLength(Mat(contours[i]), true)*0.02, true);

// Note: absolute value of an area is used because
// area may be positive or negative - in accordance with the
// contour orientation
if (approx.size() == 4 &&
fabs(contourArea(Mat(approx))) > 1000 &&
isContourConvex(Mat(approx)))
{
double maxCosine = 0;

for (int j = 2; j < 5; j++)
{
double cosine = fabs(angle(approx[j%4], approx[j-2], approx[j-1]));
maxCosine = MAX(maxCosine, cosine);
}

if (maxCosine < 0.3)
squares.push_back(approx);
}
}
}
}
}

double angle( cv::Point pt1, cv::Point pt2, cv::Point pt0 ) {
double dx1 = pt1.x - pt0.x;
double dy1 = pt1.y - pt0.y;
double dx2 = pt2.x - pt0.x;
double dy2 = pt2.y - pt0.y;
return (dx1*dx2 + dy1*dy2)/sqrt((dx1*dx1 + dy1*dy1)*(dx2*dx2 + dy2*dy2) + 1e-10);
}


and here is what i am doing in main :

vector<vector<Point> > squares;
find_squares(img, squares);
std::cout << "squares: " << squares.size() << std::endl;

draw_Squares(squares , img);

imwrite("area.png", img);
}


input image : required output image : ### Detecting rectangular part of the image

I am trying to find the rectangular regions from the image ,its drawing the rectangle's but ***getting my specific need is not obtaining*** like i show below in pics , but i am not getting any good result and also getting results slow , due to visiting every channel

Below the code for finding square :

void find_squares(Mat& image, vector<vector<Point> >& squares)
{
// blur will enhance edge detection
Mat blurred(image);
medianBlur(image, blurred, 5);

Mat gray0(blurred.size(), CV_8U), gray;
vector<vector<Point> > contours;

// find squares in every color plane of the image
//also tried one by one
for (int c = 0; c < 1; c++)
{
int ch[] = {c, 0};
mixChannels(&blurred, 1, &gray0, 1, ch, 1);

// tried several threshold levels
const int threshold_level = 2;
for (int l = 0; l < threshold_level; l++)
{
// Use Canny instead of zero threshold level!
// Canny helps to catch squares with gradient shading
if (l == 0)
{
Canny(gray0, gray, 10, 20, 3); //

// Dilate helps to remove potential holes between edge segments
dilate(gray, gray, Mat(), Point(-1,-1));
}
else
{
gray = gray0 >= (l+1) * 255 / threshold_level;
}

// Find contours and store them in a list
findContours(gray, contours, CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE);

// Test contours
vector<Point> approx;
for (size_t i = 0; i < contours.size(); i++)
{
// approximate contour with accuracy proportional
// to the contour perimeter
approxPolyDP(Mat(contours[i]), approx, arcLength(Mat(contours[i]), true)*0.02, true);

// Note: absolute value of an area is used because
// area may be positive or negative - in accordance with the
// contour orientation
if (approx.size() == 4 &&
fabs(contourArea(Mat(approx))) > 1000 &&
isContourConvex(Mat(approx)))
{
double maxCosine = 0;

for (int j = 2; j < 5; j++)
{
double cosine = fabs(angle(approx[j%4], approx[j-2], approx[j-1]));
maxCosine = MAX(maxCosine, cosine);
}

if (maxCosine < 0.3)
squares.push_back(approx);
}
}
}
}
}

double angle( cv::Point pt1, cv::Point pt2, cv::Point pt0 ) {
double dx1 = pt1.x - pt0.x;
double dy1 = pt1.y - pt0.y;
double dx2 = pt2.x - pt0.x;
double dy2 = pt2.y - pt0.y;
return (dx1*dx2 + dy1*dy2)/sqrt((dx1*dx1 + dy1*dy1)*(dx2*dx2 + dy2*dy2) + 1e-10);
}


and here is what i am doing in main :

vector<vector<Point> > squares;
find_squares(img, squares);
std::cout << "squares: " << squares.size() << std::endl;

draw_Squares(squares , img);

imwrite("area.png", img);
}


input image : required output image : ### Detecting rectangular part of the image

I am trying to find the rectangular regions from the image ,its drawing the rectangle's but ***getting my specific need is not obtaining*** like i show below in pics , and also getting results slow , due to visiting every channel

Below the code for finding square :

void find_squares(Mat& image, vector<vector<Point> >& squares)
{
// blur will enhance edge detection
Mat blurred(image);
medianBlur(image, blurred, 5);

Mat gray0(blurred.size(), CV_8U), gray;
vector<vector<Point> > contours;

// find squares in every color plane of the image
//also tried one by one
for (int c = 0; c < 1; c++)
{
int ch[] = {c, 0};
mixChannels(&blurred, 1, &gray0, 1, ch, 1);

// tried several threshold levels
const int threshold_level = 2;
for (int l = 0; l < threshold_level; l++)
{
// Use Canny instead of zero threshold level!
// Canny helps to catch squares with gradient shading
if (l == 0)
{
Canny(gray0, gray, 10, 20, 3); //

// Dilate helps to remove potential holes between edge segments
dilate(gray, gray, Mat(), Point(-1,-1));
}
else
{
gray = gray0 >= (l+1) * 255 / threshold_level;
}

// Find contours and store them in a list
findContours(gray, contours, CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE);

// Test contours
vector<Point> approx;
for (size_t i = 0; i < contours.size(); i++)
{
// approximate contour with accuracy proportional
// to the contour perimeter
approxPolyDP(Mat(contours[i]), approx, arcLength(Mat(contours[i]), true)*0.02, true);

// Note: absolute value of an area is used because
// area may be positive or negative - in accordance with the
// contour orientation
if (approx.size() == 4 &&
fabs(contourArea(Mat(approx))) > 1000 &&
isContourConvex(Mat(approx)))
{
double maxCosine = 0;

for (int j = 2; j < 5; j++)
{
double cosine = fabs(angle(approx[j%4], approx[j-2], approx[j-1]));
maxCosine = MAX(maxCosine, cosine);
}

if (maxCosine < 0.3)
squares.push_back(approx);
}
}
}
}
}

double angle( cv::Point pt1, cv::Point pt2, cv::Point pt0 ) {
double dx1 = pt1.x - pt0.x;
double dy1 = pt1.y - pt0.y;
double dx2 = pt2.x - pt0.x;
double dy2 = pt2.y - pt0.y;
return (dx1*dx2 + dy1*dy2)/sqrt((dx1*dx1 + dy1*dy1)*(dx2*dx2 + dy2*dy2) + 1e-10);
}


and here is what i am doing in main :

vector<vector<Point> > squares;
find_squares(img, squares);
std::cout << "squares: " << squares.size() << std::endl;

draw_Squares(squares , img);

imwrite("area.png", img);
}


input image : required output image : ### Detecting rectangular part of the imageError in merg and show image using C interface

I This is the code i am trying to find the rectangular regions from the using for display image ,its drawing the rectangle's using C interface in opencv

    IplImage * mSrcImg = cvLoadImage("C:\\test.jpg");
IplImage * mOutImg;
vector<IplImage*> b , g ,r;

cvSplit(&mSrcImg , &b , &g , &r , NULL);
cvEqualizeHist(&b, &b);
cvEqualizeHist(&g, &g);
cvEqualizeHist(&r, &r);

cvMerge(&b, &g, &r, NULL, &mOutImg );

cvShowImage("Image" , mOutImg);
cvWaitKey();


but ***getting my specific need is not obtaining*** like i show below getting runtime error in pics , and also getting results slow , due to visiting every channelit.

### Error in merg Impact of cubic and show image using C interfacecatmull splines on image

This is the code i I am using for display image using C interface in opencvtrying to implement some function like below from Photoshop

    IplImage * mSrcImg = cvLoadImage("C:\\test.jpg");
IplImage * mOutImg;
vector<IplImage*> b , g ,r;

cvSplit(&mSrcImg , &b , &g , &r , NULL);
cvEqualizeHist(&b, &b);
cvEqualizeHist(&g, &g);
cvEqualizeHist(&r, &r);

cvMerge(&b, &g, &r, NULL, &mOutImg );

cvShowImage("Image" , mOutImg);
cvWaitKey(); but For this I am trying to use Cubic interpolation and Catmull interpolation ( check both separately to compare the best result) , what i am not understanding is what impact these interpolation show on image and how we can get these points values where we clicked to set that curve ? and do we need to define the function these black points on the image separately ?

I am getting runtime error in it.help from these resources

Source 1

Source 2

Approx the same focus

### Impact How to extract specific area of cubic and catmull splines on image

I am trying want to implement some function count only the vignette area of this image , like below from Photoshopi have the image  For this How I am trying can count or iterate/pass through only in the area where vignette is applied and leave the other area ? I only want to use Cubic interpolation and Catmull interpolation ( check both separately to compare apply the best result) , what i am not understanding is what impact these interpolation show on image and how we can get these points values where we clicked to set that curve ? and do we need to define the function these black points algorithm on the image separately ?

area where vignette is applied , I am getting help from these resources

Source 1

Source 2

Approx tried it like give scalar to black color and extract area where it found the same focusblack color but its not working like not giving the results because color become lighter when coming towards centre of the image.

### How to extract specific area of imagepush_back is showing error

I want to count only the vignette area of this image , like i have the image Push_back is showing me error while using it with android

split(image,ch);
Mat ch0 = ch;
Mat ch1 = ch;
Mat ch2 = ch;

multiply(alpha, ch0, ch0);
multiply(alpha, ch1, ch1);
multiply(alpha, ch2, ch2);

vector<Mat> newVec;
newVec.push_back(ch0);
newVec.push_back(ch1);
newVec.push_back(ch2);

merge(newVec, image); Error :

How I can count or iterate/pass through only in the area where vignette is applied and leave the other area ? I only want to apply the algorithm on the area where vignette is applied , I tried it like give scalar to black color and extract area where it found the black color but its not working like not giving the results because color become lighter when coming towards centre of the image.

Invalid arguments ' Candidates are: void push_back(const cv::Mat &)


### push_back is showing error

Push_back is showing me error while using it with android

split(image,ch);
Mat ch0 = ch;
Mat ch1 = ch;
Mat ch2 = ch;

multiply(alpha, ch0, ch0);
multiply(alpha, ch1, ch1);
multiply(alpha, ch2, ch2);

vector<Mat> newVec;
newVec.push_back(ch0);
newVec.push_back(ch1);
newVec.push_back(ch2);

merge(newVec, image);


Error :Error:

Invalid arguments ' Candidates are: void push_back(const cv::Mat &)


### push_back Lining in output image

Vignette is not showing error

Push_back is showing me error while as it should be on android using it with android ndk , below the code I tried

split(image,ch);
void Vignete(Mat& img1, Mat& img2, Mat& out)
{

resize(img1, img1, img2.size());
img1.convertTo(img1,CV_32FC4,1.0/255.0);
img2.convertTo(img2, CV_32FC4, 1.0/255.0);
vector<Mat> ch;
Mat I1;
split(img2,ch);
Mat alpha1 = ch;
Mat alpha = alpha1.clone();
Mat ch0 = ch;
Mat ch1 = ch;
Mat ch2         Mat ch_2 = ch;

multiply(alpha, ch0, ch0);
multiply(alpha, ch1, ch1);
multiply(alpha, ch2, ch2);

Mat ch_3 = ch;
cv::multiply(alpha,ch0,ch0);
cv::multiply(alpha,ch1,ch1);
cv::multiply(alpha,ch_2,ch_2);
cv::multiply(alpha,ch_3,ch_3);
vector<Mat> newVec;
newVec.push_back(ch0);
newVec.push_back(ch1);
newVec.push_back(ch2);

newVec.push_back(ch_2);
newVec.push_back(ch_3);
merge(newVec, image);
I1);
I1.convertTo(out, CV_8UC4, 255);
}


Error:

Invalid arguments ' Candidates are: void push_back(const cv::Mat &)

Input img2 is : But the output I am getting on my android device is Why I am getting these linings ?

### Lining in on output image

Vignette is not showing as it should be on android using ndk , below the code I tried

void Vignete(Mat& img1, Mat& img2, Mat& out)
{

resize(img1, img1, img2.size());
img1.convertTo(img1,CV_32FC4,1.0/255.0);
img2.convertTo(img2, CV_32FC4, 1.0/255.0);
vector<Mat> ch;
Mat I1;
split(img2,ch);
Mat alpha1 = ch;
Mat alpha = alpha1.clone();
Mat ch0 = ch;
Mat ch1 = ch;
Mat ch_2 = ch;
Mat ch_3 = ch;
cv::multiply(alpha,ch0,ch0);
cv::multiply(alpha,ch1,ch1);
cv::multiply(alpha,ch_2,ch_2);
cv::multiply(alpha,ch_3,ch_3);
vector<Mat> newVec;
newVec.push_back(ch0);
newVec.push_back(ch1);
newVec.push_back(ch_2);
newVec.push_back(ch_3);
merge(newVec, I1);
I1.convertTo(out, CV_8UC4, 255);
}


Input img2 is : But the output I am getting on my android device is Why I am getting these linings ?

### Lining on output imageImage call from android to opencv

Vignette I am using android ndk with opencv , I am giving two images as input and then one as output , my 2 input images are one is not showing as it should be on android using ndk image and second is vignette , I am getting some kind of logical error from java side , below the is my code I triedof java

public void Vignete(Mat& img1, Mat& img2, Mat& out)
onCreate(Bundle savedInstanceState)
{

resize(img1, img1, img2.size());
img1.convertTo(img1,CV_32FC4,1.0/255.0);
img2.convertTo(img2, CV_32FC4, 1.0/255.0);
vector<Mat> ch;
Mat I1;
split(img2,ch);
Mat alpha1 = ch;
Mat alpha = alpha1.clone();
Mat ch0 = ch;
Mat ch1 = ch;
Mat ch_2 = ch;
Mat ch_3 = ch;
cv::multiply(alpha,ch0,ch0);
cv::multiply(alpha,ch1,ch1);
cv::multiply(alpha,ch_2,ch_2);
cv::multiply(alpha,ch_3,ch_3);
vector<Mat> newVec;
newVec.push_back(ch0);
newVec.push_back(ch1);
newVec.push_back(ch_2);
newVec.push_back(ch_3);
merge(newVec, I1);
I1.convertTo(out, CV_8UC4, 255);
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

imageview_1=(ImageView) findViewById(R.id.imageView1);
imageview_2=(ImageView) findViewById(R.id.imageView2);

InputStream is , Vign;
is = this.getResources().openRawResource(R.drawable.me);
final Bitmap bmInImg = BitmapFactory.decodeStream(is);
Vign = this.getResources().openRawResource(R.drawable.p);
final Bitmap bmInImg2 = BitmapFactory.decodeStream(Vign);

mPhotoIntArray = new int[bmInImg.getWidth() * bmInImg.getHeight()];
nPhotoIntArray = new int[bmInImg.getWidth() * bmInImg.getHeight()];
vPhotoIntArray = new int[bmInImg2.getWidth() * bmInImg2.getHeight()];
imageview_1.setImageBitmap(bmInImg);
// Copy pixel data from the Bitmap into the 'intArray' array
bmInImg.getPixels(mPhotoIntArray, 0, bmInImg.getWidth(), 0, 0, bmInImg.getWidth(), bmInImg.getHeight());
bmInImg2.getPixels(vPhotoIntArray, 0, bmInImg2.getWidth(), 0, 0, bmInImg2.getWidth(), bmInImg2.getHeight());

mCannyOutArray = new int[bmInImg2.getWidth() * bmInImg2.getHeight()];
final Bitmap bmOutImg = Bitmap.createBitmap(bmInImg2.getWidth(), bmInImg2.getHeight(), Config.ARGB_8888);
bmOutImg.setPixels(mCannyOutArray, 0, bmInImg2.getWidth(), 0, 0, bmInImg2.getWidth(), bmInImg2.getHeight());

Button button= (Button) findViewById(R.id.NextButton);
button.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v)  {

if (count ==0)
{
Vig(bmInImg.getHeight(),bmInImg.getWidth(),bmInImg2.getHeight(),bmInImg2.getWidth(), mPhotoIntArray,vPhotoIntArray, mCannyOutArray);

bmOutImg.setPixels(mCannyOutArray, 0, bmInImg2.getWidth(), 0, 0, bmInImg2.getWidth(), bmInImg2.getHeight());
imageview_2.setImageBitmap(bmOutImg);
}

count++;
}
});

String extStorageDirectory = Environment.getExternalStorageDirectory().toString();
String outFileName = extStorageDirectory + "/me.png";
OutputBitmapToFile(bmOutImg, outFileName);
}


Input img2 Above code give me vignette fine but the first image output is :showing lininng on all image and when i add both vignette look fine but image inside is full of lines , if i change the code of bmOutImg.setPixels from outside and inside condition if to

Vig(bmInImg.getHeight(),bmInImg.getWidth(),bmInImg2.getHeight(),bmInImg2.getWidth(), mPhotoIntArray,vPhotoIntArray, mCannyOutArray);

bmOutImg.setPixels(mCannyOutArray, 0, bmInImg.getWidth(), 0, 0, bmInImg.getWidth(), bmInImg.getHeight());
imageview_2.setImageBitmap(bmOutImg); Then it shows lining on vignette image. What should be my mCannyOutArray should be so that my both images show satisfactory result for my output.

But the output I am getting on my android device is Why I am getting these linings ?