Ask Your Question
1

How can I resize an image from the center?\

asked 2015-08-04 06:50:11 -0500

marcoE gravatar image

Hello! I have a contour, and I want to resize this contour and then draw the resized contour in same image, In another words, I want to create an external contour to the existent one. To this I need to resize the contour mask. however, when I draw both I'm in trouble, because the resized one it is not centered. How can I make the resize from the image center?

 height, width = lA1innerContourmask.shape[:2]
 nwid =int( 1.5 * width)
 nh = int(1.5 * height)
 res = cv2.resize(lA1innerContourmask,(nwid, nh),0,0, interpolation = cv2.INTER_CUBIC)

# find contour of the resized

resizedcontour, hier_ = cv2.findContours(res,cv2.RETR_CCOMP,
                                        cv2.CHAIN_APPROX_SIMPLE)

cv2.drawContours(lA1innerContourmask,resizedcontour,-1,(0,0,255),1) 
outax = np.hstack([lA1innerContourmask])    
plt.imshow(outax, 'gray')
plt.show()
edit retag flag offensive close merge delete

Comments

I think you are doing it in a complicated manner. Have you seen this?

thdrksdfthmn gravatar imagethdrksdfthmn ( 2015-08-04 07:27:20 -0500 )edit

Yes I have seen. The principles are the same. The problem is the image center. If I put a scalled contour over the original one they not alligned. The center is not the same, like this:https://dl.dropboxusercontent.com/u/710615/figure_1.png

marcoE gravatar imagemarcoE ( 2015-08-04 07:52:00 -0500 )edit

I "just" want to create an external contour to the existing one. Imagine that I have the interior, and then I want to create an external at ds distance. https://en.wikipedia.org/wiki/Time_ev...

marcoE gravatar imagemarcoE ( 2015-08-04 09:18:47 -0500 )edit
1

So you want to create the outbound contour so it will vary in shape? I do not know how to do that, but if you want to just duplicate it and make it a little bigger, then you can just do a new contour that is the same as the resized initial (for (cv::Point p in contour) { outboundContour.push_back(cv::Point(p.x * ds, p.y * ds)); })

thdrksdfthmn gravatar imagethdrksdfthmn ( 2015-08-04 09:40:44 -0500 )edit

Yes, that's what i want to do, I don't know how to do either. I think that your code won't work, in particular in the arcs

marcoE gravatar imagemarcoE ( 2015-08-04 09:57:57 -0500 )edit

AA, so you need to create a contour of the contour? Then draw it in a new mat and then apply find contour. But it is strange that you want to have the contours, and the contours of contours... why would you have an arc (contour not closed)?

thdrksdfthmn gravatar imagethdrksdfthmn ( 2015-08-04 10:21:25 -0500 )edit

YesI want to create a contour of the contour. Then I'll fit both in a rectangle box. Both contours will became holes of the rectangle. What I want meant with "arc" was the semi-circle perimeter.

marcoE gravatar imagemarcoE ( 2015-08-04 10:24:15 -0500 )edit

Normally if it is an area, you can detect its contour, and you can do the erosion (if you do not like the my resize thing) on the contour. More you are saying in a comment of your answer that you do not want a contour of a contour...

thdrksdfthmn gravatar imagethdrksdfthmn ( 2015-08-05 02:46:28 -0500 )edit

2 answers

Sort by ยป oldest newest most voted
4

answered 2015-08-04 10:32:44 -0500

updated 2015-08-04 11:02:39 -0500

i am not sure if i understand rigth but maybe cv::erode helps to solve your problem

image description

image description

#include "opencv2/imgcodecs.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace cv;
using namespace std;
Mat src; Mat src_gray;
int thresh = 100;
int max_thresh = 255;

int main( int, char** argv )
{
  src = imread("25912.png");
  if (src.empty())
  {
    cerr << "No image supplied ..." << endl;
    return -1;
  }
  cvtColor( src, src_gray, COLOR_BGR2GRAY );
  blur( src_gray, src_gray, Size(3,3) );
  const char* source_window = "Source";
  namedWindow( source_window, WINDOW_AUTOSIZE );


  Mat canny_output;
  vector<vector<Point> > contours;
  vector<Vec4i> hierarchy;

  // you can change parameters to your result 
  erode(src_gray,src_gray, Mat(), Point(-1,-1),5);

  Canny( src_gray, canny_output, thresh, thresh*2, 3 );
  findContours( canny_output, contours, hierarchy, RETR_TREE, CHAIN_APPROX_SIMPLE, Point(0, 0) );
  for( size_t i = 0; i< contours.size(); i++ )
     {
       Scalar color = Scalar( 0,255,0 );
       drawContours( src, contours, (int)i, color, 2, 8, hierarchy, 0, Point() );
     }
  imshow( source_window, src );

  waitKey(0);
  return(0);
}

your image's result: image description

edit flag offensive delete link more

Comments

Thank you for the answer. The final result is more less what I want. But I don't have solid shapes, only a contour. I'm not sure if could apply all the process . (I'm trying to translate to python :) )

This is my source image:

https://dl.dropboxusercontent.com/u/7...

marcoE gravatar imagemarcoE ( 2015-08-04 10:49:26 -0500 )edit

Thank you, I'm trying translate to python. There some differences. I didn't understand some options that you take such as the blur or erode; so it is more dificult to set up the parameters on my python code

marcoE gravatar imagemarcoE ( 2015-08-05 04:54:03 -0500 )edit
1

in your case no need to cv::blur. but cv::erode is key function. you can share your python code. i hope someone help you to translate python.

sturkmen gravatar imagesturkmen ( 2015-08-05 05:08:18 -0500 )edit

I've already made an answer in python, but I can only post tomorrow (forum rules) Thank you.

I just have a doubt Mat() in erode is equivalent to what kind of kernel? http://docs.opencv.org/modules/imgpro...

marcoE gravatar imagemarcoE ( 2015-08-05 05:57:06 -0500 )edit

@sturkmen how can I control the edge's width? What can I do to be more distant of the initial contour? Another question: If you notice, my Initial contour has some noise. Some lines are not straight. How can I filter? Thank you!

marcoE gravatar imagemarcoE ( 2015-08-07 05:53:48 -0500 )edit

in c++ sample you can increas 5 to any value erode(src_gray,src_gray, Mat(), Point(-1,-1),5); higher values cause some loss of details

sturkmen gravatar imagesturkmen ( 2015-08-07 06:13:02 -0500 )edit
marcoE gravatar imagemarcoE ( 2015-08-07 06:47:37 -0500 )edit
0

answered 2015-08-06 08:09:33 -0500

marcoE gravatar image

Based on @sturmen code I translated to python. I'm not sure about the Mat() instruction, what is the equivalent in python.

blur = cv2.blur(lA1innerContourmask, (3,3))

kernel = np.ones((5,5),np.uint8)
erodeblur = cv2.erode(blur,kernel,(-1,-1), iterations=5)

edges = cv2.Canny(erodeblur,100,200,6)

contoursCanny, hier__ = cv2.findContours(edges,cv2.RETR_TREE,
                                            cv2.CHAIN_APPROX_SIMPLE, offset = (0,0))
cv2.imshow('new image',blur)
cv2.waitKey(0)

cv2.imshow('new image',edges)
cv2.waitKey(0)

cv2.drawContours(lA1innerContourmask,contoursCanny,-1,(0,0,255),2,8,hier__,0)

cv2.imshow('new image',lA1innerContourmask)
cv2.waitKey(0)
edit flag offensive delete link more
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2015-08-04 06:50:11 -0500

Seen: 2,703 times

Last updated: Aug 06 '15