# Edge detection and contour drawing on some part of the image

**URL:** <https://forum.opencv.org/t/edge-detection-and-contour-drawing-on-some-part-of-the-image/17060>\
**Category:** Uncategorized\
**Tags:** programming, canny\
**Created:** [March 28, 2024, 5:47am UTC](https://forum.opencv.org/t/edge-detection-and-contour-drawing-on-some-part-of-the-image/17060 "2024-03-28T05:47:44Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Manz](https://avatars.discourse-cdn.com/v4/letter/m/f05b48/32.png) [@Manz](https://forum.opencv.org/u/Manz)\
**Post date:** [March 28, 2024, 5:47am UTC](https://forum.opencv.org/t/edge-detection-and-contour-drawing-on-some-part-of-the-image/17060/1 "2024-03-28T05:47:45Z")

</div>

![Screenshot 2024-03-27 154222](https://us1.discourse-cdn.com/flex020/uploads/opencv/original/2X/1/1c7a2b4fd5723cba1c584e2d6cfa0ac99b42bc0f.png)  
the output u can see over there the edges on some of the window are discontinued which i need to detect and draw contour because of the tree being overlaid on them . #include   
#include \<opencv2/opencv.hpp\>

using namespace std;  
using namespace cv;

int main(){

```
Mat input=imread("C:/Users/manjswa2/Downloads/Basic_Linear_Transform_Tutorial_gamma_correction.jpg");

Mat lookUpTable(1, 256, CV_8U);
uchar* p = lookUpTable.ptr();
for (int i = 0; i < 256; ++i)
    p[i] = saturate_cast<uchar>(pow(i / 255.0, 0.8) * 255.0);
Mat res = input.clone();
LUT(input, lookUpTable, res);

Mat imagray,edgeout,fout,edgeout1;
cvtColor(res,imagray,COLOR_RGB2GRAY);
GaussianBlur(res,fout,Size(3,3),0);

Canny(imagray,edgeout,150,200);

Mat kernel = getStructuringElement(MORPH_RECT, Size(3, 3));
morphologyEx(edgeout, edgeout1, MORPH_CLOSE, kernel);

```

vector\<vector\> contours;  
findContours(edgeout1, contours, RETR\_EXTERNAL, CHAIN\_APPROX\_NONE);  
// draw contours on windows  
vector\<vector\> squares;  
for (size\_t i = 0; i \< contours.size(); i++) {  
vector approx;  
approxPolyDP(contours[i], approx, arcLength(contours[i], true) \* 0.0225,true);  
if (approx.size() == 4 && isContourConvex(approx) ) {

```
        double length = arcLength(approx, true);
        if (length > 50) { // Adjust the minimum length as per your requirement
        squares.push_back(approx);
    }
}
}

// Draw contour on the original image
Mat image_copy = res.clone();

drawContours(image_copy, squares, -1, Scalar(0, 255, 0), 2);

imshow("Input",input);
imshow("Cannyopt",edgeout1);
imshow("Final contour",image_copy);

waitKey(0);

```

return 0;  
} this is my code what changes can i make out to get it right
