How to find and draw the outer lines of an image in OpenCV?


Mr Programmer

enter image description here

As i process the image(BEFORE) then i need its outer border and draw red line in openCV android (like AFTER image),

If anyone knows about it, please let me know about advanced tanks.

Costantino Grana

This is a classic application of cv::findContours .

A working example is provided in the documentation .

For your specific situation, you can do this:

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>

int main(int argc, char** argv)
{
    using namespace cv;
    using namespace std;

    Mat3b image;
    image = imread(argv[1], 1);  
    imshow ("Before", image);

    Mat1b gray;
    cvtColor(image, gray, CV_BGR2GRAY);
    threshold(gray, gray, 250, 255, THRESH_BINARY);
    medianBlur(gray,gray, 3);
    gray = 255-gray;

    vector<vector<Point> > contours;
    vector<Vec4i> hierarchy;
    findContours(gray, contours, hierarchy, RETR_EXTERNAL , CHAIN_APPROX_NONE);

    Scalar color = Scalar( 0, 0, 255 );
    for (size_t i = 0; i < contours.size(); i++) {
        drawContours(image, contours, i, color, 2, 8, hierarchy, 0, Point());
    }     
    imshow("After", image);
    return 0;
}

This will give you this output:Sample output

Related


Find extreme outer points in an image using Python OpenCV

coffeewin: I have an image of this statue. I'm trying to find the topmost, bottommost, leftmost and rightmost points on a statue. Is there a way to measure the edge of each side to determine the outermost point on the statue? I want to get the (x,y)coordinates

How to draw lines on an image in OpenCV?

Rahul: If I have polar coordinates of a line, how can I draw it on an image in OpenCV and python? LineThe function takes 2 points, but only draws line segments. I want to draw a line from one edge of an image to the other. Robert Caspary: Just count 2 points.

How to draw transparent lines on an image using PIL?

Retric Here is what I get: draw.line([(x1,y1),(x2,y2)],fill = (255, 255, 255)) The result is a white line, but I want a transparent line, what should I do? Kay Patel As far as I know, the line function takes RGB or RGBA values. try: draw.line([(x1,y1),(x2,y2)

How to draw lines on an image on a tkinter window?

smoked The program reads the image located at C:/Square.png and draws lines on it. Also defines the plot title. I want to display the whole image in a tkinter window. what should I do? Here is the image. The name has to be changed and we can run the code. http

How to draw lines on an image in OpenCV?

Rahul: If I have polar coordinates of a line, how can I draw it on an image in OpenCV and python? LineThe function takes 2 points, but only draws line segments. I want to draw a line from one edge of an image to the other. Robert Caspary: Just count 2 points.

How to draw lines on an image using matplotlib?

KhoaLearnToCode I'm having a problem here when trying to draw multiple lines on an image. So, here is my code: fig=plt.figure() final = cv2.imread('frame9.jpg') for p in polar: plt.plot([0,1],[p[0],p[0]],c='red') plt.imshow(final,cmap=plt.cm.gray) plt.show

How to draw perfect lines in python using opencv

Trojan War I am trying to draw perfect lines using mouse events by clicking and dragging the mouse. The problem is that multiple lines are printed when plotting. Here is the code I've been testing. import cv2 import numpy as np drawing = False x1,y1 = -1,-1

draw lines and distances on image opencv python

user 300058 I'm having a problem with this: I can't draw a line on an image with a certain color, nor can I find out the distance to the place. Help make it look like the image below: my code: import cv2 import numpy as np from PIL import ImageGrab whil

How to draw transparent lines on an image using PIL?

Retric Here is what I get: draw.line([(x1,y1),(x2,y2)],fill = (255, 255, 255)) The result is a white line, but I want a transparent line, what should I do? Kay Patel As far as I know, the line function takes RGB or RGBA values. try: draw.line([(x1,y1),(x2,y2)

How to draw transparent lines on an image using PIL?

Retric Here is what I get: draw.line([(x1,y1),(x2,y2)],fill = (255, 255, 255)) The result is a white line, but I want a transparent line, what should I do? Kay Patel As far as I know, the line function takes RGB or RGBA values. try: draw.line([(x1,y1),(x2,y2)

How to find and draw the outer lines of an image in OpenCV?

Mr Programmer As i process the image(BEFORE) then i need its outer border and draw red line in openCV android (like AFTER image), If anyone knows about it, please let me know about advanced tanks. Costantino Grana This is a classic application of cv::findConto

How to draw transparent lines on an image using PIL?

Retric Here is what I get: draw.line([(x1,y1),(x2,y2)],fill = (255, 255, 255)) The result is a white line, but I want a transparent line, what should I do? Kay Patel As far as I know, the line function takes RGB or RGBA values. try: draw.line([(x1,y1),(x2,y2)

Find lines of an image using opencv LineSegmentDetector

Nasim I have color image and should use opencv LineSegmentDetector algorithm to detect lines of rectangles in image this is my picture: I am using this code: import cv2 img = cv2.imread("rectangles.jpg",0) #Create default parametrization LSD lsd = cv2.createL

How to draw lines between points in OpenCV?

Open V I have a set of tuples: a = [(375, 193) (364, 113) (277, 20) (271, 16) (52, 106) (133, 266) (289, 296) (372, 282)] How to draw lines between points in OpenCV? Here is my code that doesn't work: for index, item in enumerate(a): print (item[index])

How to draw lines on an image on a tkinter window?

smoked The program reads the image located at C:/Square.png and draws lines on it. Also defines the plot title. I want to display the whole image in a tkinter window. what should I do? Here is the image. The name has to be changed and we can run the code. http

How to draw lines on an image using matplotlib?

KhoaLearnToCode I'm having a problem here when trying to draw multiple lines on an image. So, here is my code: fig=plt.figure() final = cv2.imread('frame9.jpg') for p in polar: plt.plot([0,1],[p[0],p[0]],c='red') plt.imshow(final,cmap=plt.cm.gray) plt.show

How to draw lines on OpenCV Camera Preview?

McMad I am using OpenCV camera preview. I want to draw a rectangle on live preview. I've tried to override the ondraw method but I can't see a line. please help. package com.example.easymeasure; import org.opencv.android.JavaCameraView; import android.conten

How to draw curved and straight lines in one image?

stack I'm trying to draw a rectangle that is basically a circle, with a square on one end. Kind of like a battery. I have some limitations, basically this can only be done with a single paint object and should be able to support strokes. Here is my code so far

Find extreme outer points in an image using Python OpenCV

coffeewin: I have an image of this statue. I'm trying to find the topmost, bottommost, leftmost and rightmost points on a statue. Is there a way to measure the edge of each side to determine the outermost point on the statue? I want to get the (x,y)coordinates

How to draw lines on an image in OpenCV?

Rahul: If I have polar coordinates of a line, how can I draw it on an image in OpenCV and python? LineThe function takes 2 points, but only draws line segments. I want to draw a line from one edge of an image to the other. Robert Caspary: Just count 2 points.

How to draw lines on an image using matplotlib?

KhoaLearnToCode I'm having a problem here when trying to draw multiple lines on an image. So, here is my code: fig=plt.figure() final = cv2.imread('frame9.jpg') for p in polar: plt.plot([0,1],[p[0],p[0]],c='red') plt.imshow(final,cmap=plt.cm.gray) plt.show

How to draw lines on an image on a tkinter window?

smoked The program reads the image located at C:/Square.png and draws lines on it. Also defines the plot title. I want to display the whole image in a tkinter window. what should I do? Here is the image. The name has to be changed and we can run the code. http

How to draw transparent lines on an image using PIL?

Retric Here is what I get: draw.line([(x1,y1),(x2,y2)],fill = (255, 255, 255)) The result is a white line, but I want a transparent line, what should I do? Kay Patel As far as I know, the line function takes RGB or RGBA values. try: draw.line([(x1,y1),(x2,y2)

How to draw transparent lines on an image using PIL?

Retric Here is what I get: draw.line([(x1,y1),(x2,y2)],fill = (255, 255, 255)) The result is a white line, but I want a transparent line, what should I do? Kay Patel As far as I know, the line function takes RGB or RGBA values. try: draw.line([(x1,y1),(x2,y2)

Find lines of an image using opencv LineSegmentDetector

Nasim I have color image and should use opencv LineSegmentDetector algorithm to detect lines of rectangles in image this is my picture: I am using this code: import cv2 img = cv2.imread("rectangles.jpg",0) #Create default parametrization LSD lsd = cv2.createL

How to draw lines on an image on a tkinter window?

smoked The program reads the image located at C:/Square.png and draws lines on it. Also defines the plot title. I want to display the whole image in a tkinter window. what should I do? Here is the image. The name has to be changed and we can run the code. http

How to draw lines on an image using matplotlib?

KhoaLearnToCode I'm having a problem here when trying to draw multiple lines on an image. So, here is my code: fig=plt.figure() final = cv2.imread('frame9.jpg') for p in polar: plt.plot([0,1],[p[0],p[0]],c='red') plt.imshow(final,cmap=plt.cm.gray) plt.show