SlideShare a Scribd company logo
1 of 9
Download to read offline
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include "opencv2/core/opengl.hpp"
// #include "t0.cpp"
// #include "t1.cpp"
using namespace std;
using namespace cv;
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image/stb_image.h"
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "stb_image/stb_image_write.h"
cv::Mat out;
cv::Mat img = cv::imread("fish.png");
void contours_algo()
{
std::vector> contours;
// cv::namedWindow("In",0);
// cv::namedWindow("Out",0);
std::vector hierarchy;
cv::findContours(out,contours,hierarchy,cv::RETR_LIST,cv::CHAIN_APPROX_SIMPLE);
cv::Mat mask = cv::Mat::zeros(img.size(),CV_8UC3);
cv::drawContours(mask,contours,-1,cv::Scalar(255,255,255),5);
contours.push_back(vector());
hierarchy.push_back(cv::Vec4i());
cv::imshow("In",img);
cv::imshow("Mask", mask);
cv::imshow("Out",out);
cv::waitKey(0);
}
float perpendicularDistance(const Point& p, const Point& lineStart, const Point& lineEnd) {
float x = p.x;
float y = p.y;
float x1 = lineStart.x;
float y1 = lineStart.y;
float x2 = lineEnd.x;
float y2 = lineEnd.y;
float A = x - x1;
float B = y - y1;
float C = x2 - x1;
float D = y2 - y1;
float dot = A *C + B *D;
float len_sq = C * C + D * D;
float param = dot / len_sq;
float xx, yy;
if (param < 0) {
xx = x1;
yy = y1;
} else if (param > 1) {
xx = x2;
yy = y2;
} else {
xx = x1 + param * C;
yy = y1 + param * D;
}
float dx = x - xx;
float dy = y - yy;
return sqrt(dx * dx + dy * dy);
}
void simplifyContourRDP(const vector& contour, int startIdx, int endIdx, float epsilon, vector&
simplifiedContour) {
if (endIdx <= startIdx + 1) {
// Base case: only two points in the line segment
return;
}
float maxDistance = 0.0;
int maxDistanceIdx = 0;
Point lineStart = contour[startIdx];
Point lineEnd = contour[endIdx];
// Find the point with the maximum perpendicular distance
// from the line segment
for (int i = startIdx + 1; i < endIdx; ++i) {
float distance = perpendicularDistance(contour[i], lineStart, lineEnd);
if (distance > maxDistance) {
maxDistance = distance;
maxDistanceIdx = i;
}
}
// If the maximum distance is greater than epsilon,
// include the point in the simplified contour
if (maxDistance > epsilon) {
simplifiedContour.push_back(contour[maxDistanceIdx]);
// Simplify the left and right sub-contours
simplifyContourRDP(contour, startIdx, maxDistanceIdx, epsilon, simplifiedContour);
simplifyContourRDP(contour, maxDistanceIdx, endIdx, epsilon, simplifiedContour);
}
}
vector simplifyContourRDP(const vector& contour, float epsilon) {
vector simplifiedContour;
simplifiedContour.push_back(contour.front());
simplifiedContour.push_back(contour.back());
simplifyContourRDP(contour, 0, contour.size() - 1, epsilon, simplifiedContour);
return simplifiedContour;
}
int main(int argc , char **argv)
{
cv::Mat bilateralimg,medianimg,out;
cv::Mat edges = cv::imread("fish.png", cv::IMREAD_GRAYSCALE);
cv::Mat img = cv::imread("fish.png");
cv::Mat img2 = cv::imread("fish.png",cv::IMREAD_GRAYSCALE);
Mat grayimg;
cvtColor(img, grayimg, cv::COLOR_BGR2GRAY);
Mat binaryImage;
threshold(grayimg, binaryImage, 128, 255, THRESH_BINARY);
vector> contours;
findContours(binaryImage, contours, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE);
vector> simplifiedContours;
float epsilon = 10.0; // Adjust this value to control the level of simplification
for (const auto& contour : contours) {
vector simplifiedContour = simplifyContourRDP(contour, epsilon);
simplifiedContours.push_back(simplifiedContour);
}
Mat resultImage = img.clone();
drawContours(resultImage, simplifiedContours, -1, Scalar(0, 0, 255), 2);
// Display the result
imshow("Simplified Contours", resultImage);
waitKey(0);
return 0;
cv::namedWindow("In",0);
cv::namedWindow("Out",0);
cv::medianBlur(img,medianimg,15);
if (img.empty() || img2.empty())
{
std::cerr << "Error loading image(s)" << std::endl;
return 1;
}
cv::Canny(img,out,70,300);
cv::threshold(img,out,100,255,cv::THRESH_BINARY);
cv::namedWindow("In",0);
cv::namedWindow("Out",0);
contours_algo();
cv::imshow("window",img);
cv::imshow("window2GRAY_SCALED",img2);
cv::imshow("Median",medianimg);
cv::imshow("In",img);
cv::imshow("Out",out);
cv::waitKey(0);
return 0;
}
In this code I am getting segmentation fault(core dumped) error please fix this.

More Related Content

Similar to #include bitsstdc++.h#include unistd.h#include GLglew.h.pdf

Can anyone fix this code, I use Visual StudiosThe Errors im getti.pdf
Can anyone fix this code, I use Visual StudiosThe Errors im getti.pdfCan anyone fix this code, I use Visual StudiosThe Errors im getti.pdf
Can anyone fix this code, I use Visual StudiosThe Errors im getti.pdf
arjunhassan8
 
include ltiostreamgt include ltstringgt include .pdf
include ltiostreamgt include ltstringgt include .pdfinclude ltiostreamgt include ltstringgt include .pdf
include ltiostreamgt include ltstringgt include .pdf
contact32
 
Can you finish and write the int main for the code according to the in.pdf
Can you finish and write the int main for the code according to the in.pdfCan you finish and write the int main for the code according to the in.pdf
Can you finish and write the int main for the code according to the in.pdf
aksachdevahosymills
 
#include iostream #include deque #include stdio.h   scan.pdf
#include iostream #include deque #include stdio.h   scan.pdf#include iostream #include deque #include stdio.h   scan.pdf
#include iostream #include deque #include stdio.h   scan.pdf
anandmobile
 
C++: Constructor, Copy Constructor and Assignment operator
C++: Constructor, Copy Constructor and Assignment operatorC++: Constructor, Copy Constructor and Assignment operator
C++: Constructor, Copy Constructor and Assignment operator
Jussi Pohjolainen
 

Similar to #include bitsstdc++.h#include unistd.h#include GLglew.h.pdf (20)

unit 3 ppt.pptx
unit 3 ppt.pptxunit 3 ppt.pptx
unit 3 ppt.pptx
 
U3.pptx
U3.pptxU3.pptx
U3.pptx
 
Can anyone fix this code, I use Visual StudiosThe Errors im getti.pdf
Can anyone fix this code, I use Visual StudiosThe Errors im getti.pdfCan anyone fix this code, I use Visual StudiosThe Errors im getti.pdf
Can anyone fix this code, I use Visual StudiosThe Errors im getti.pdf
 
Pointers in C Language
Pointers in C LanguagePointers in C Language
Pointers in C Language
 
Computer graphics
Computer graphicsComputer graphics
Computer graphics
 
Programming ppt files (final)
Programming ppt files (final)Programming ppt files (final)
Programming ppt files (final)
 
Css5 canvas
Css5 canvasCss5 canvas
Css5 canvas
 
Arrry structure Stacks in data structure
Arrry structure Stacks  in data structureArrry structure Stacks  in data structure
Arrry structure Stacks in data structure
 
Assignment c programming
Assignment c programmingAssignment c programming
Assignment c programming
 
include ltiostreamgt include ltstringgt include .pdf
include ltiostreamgt include ltstringgt include .pdfinclude ltiostreamgt include ltstringgt include .pdf
include ltiostreamgt include ltstringgt include .pdf
 
Useful c programs
Useful c programsUseful c programs
Useful c programs
 
CBSE Class XII Comp sc practical file
CBSE Class XII Comp sc practical fileCBSE Class XII Comp sc practical file
CBSE Class XII Comp sc practical file
 
Constants and Unformatted Input Output Functions.pptx
Constants and Unformatted Input Output Functions.pptxConstants and Unformatted Input Output Functions.pptx
Constants and Unformatted Input Output Functions.pptx
 
Can you finish and write the int main for the code according to the in.pdf
Can you finish and write the int main for the code according to the in.pdfCan you finish and write the int main for the code according to the in.pdf
Can you finish and write the int main for the code according to the in.pdf
 
Write a program that reads a connected graph from a file and displays.docx
 Write a program that reads a connected graph from a file and displays.docx Write a program that reads a connected graph from a file and displays.docx
Write a program that reads a connected graph from a file and displays.docx
 
#include iostream #include deque #include stdio.h   scan.pdf
#include iostream #include deque #include stdio.h   scan.pdf#include iostream #include deque #include stdio.h   scan.pdf
#include iostream #include deque #include stdio.h   scan.pdf
 
C++: Constructor, Copy Constructor and Assignment operator
C++: Constructor, Copy Constructor and Assignment operatorC++: Constructor, Copy Constructor and Assignment operator
C++: Constructor, Copy Constructor and Assignment operator
 
Writeup advanced lane_lines_project
Writeup advanced lane_lines_projectWriteup advanced lane_lines_project
Writeup advanced lane_lines_project
 
Chainer-Compiler 動かしてみた
Chainer-Compiler 動かしてみたChainer-Compiler 動かしてみた
Chainer-Compiler 動かしてみた
 
Presentation for structure in c
Presentation for  structure in cPresentation for  structure in c
Presentation for structure in c
 

Recently uploaded

SURVEY I created for uni project research
SURVEY I created for uni project researchSURVEY I created for uni project research
SURVEY I created for uni project research
CaitlinCummins3
 
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
中 央社
 

Recently uploaded (20)

male presentation...pdf.................
male presentation...pdf.................male presentation...pdf.................
male presentation...pdf.................
 
Graduate Outcomes Presentation Slides - English (v3).pptx
Graduate Outcomes Presentation Slides - English (v3).pptxGraduate Outcomes Presentation Slides - English (v3).pptx
Graduate Outcomes Presentation Slides - English (v3).pptx
 
VAMOS CUIDAR DO NOSSO PLANETA! .
VAMOS CUIDAR DO NOSSO PLANETA!                    .VAMOS CUIDAR DO NOSSO PLANETA!                    .
VAMOS CUIDAR DO NOSSO PLANETA! .
 
Spring gala 2024 photo slideshow - Celebrating School-Community Partnerships
Spring gala 2024 photo slideshow - Celebrating School-Community PartnershipsSpring gala 2024 photo slideshow - Celebrating School-Community Partnerships
Spring gala 2024 photo slideshow - Celebrating School-Community Partnerships
 
Including Mental Health Support in Project Delivery, 14 May.pdf
Including Mental Health Support in Project Delivery, 14 May.pdfIncluding Mental Health Support in Project Delivery, 14 May.pdf
Including Mental Health Support in Project Delivery, 14 May.pdf
 
8 Tips for Effective Working Capital Management
8 Tips for Effective Working Capital Management8 Tips for Effective Working Capital Management
8 Tips for Effective Working Capital Management
 
When Quality Assurance Meets Innovation in Higher Education - Report launch w...
When Quality Assurance Meets Innovation in Higher Education - Report launch w...When Quality Assurance Meets Innovation in Higher Education - Report launch w...
When Quality Assurance Meets Innovation in Higher Education - Report launch w...
 
SURVEY I created for uni project research
SURVEY I created for uni project researchSURVEY I created for uni project research
SURVEY I created for uni project research
 
OSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsOSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & Systems
 
e-Sealing at EADTU by Kamakshi Rajagopal
e-Sealing at EADTU by Kamakshi Rajagopale-Sealing at EADTU by Kamakshi Rajagopal
e-Sealing at EADTU by Kamakshi Rajagopal
 
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
 
UChicago CMSC 23320 - The Best Commit Messages of 2024
UChicago CMSC 23320 - The Best Commit Messages of 2024UChicago CMSC 23320 - The Best Commit Messages of 2024
UChicago CMSC 23320 - The Best Commit Messages of 2024
 
24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...
24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...
24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...
 
OS-operating systems- ch05 (CPU Scheduling) ...
OS-operating systems- ch05 (CPU Scheduling) ...OS-operating systems- ch05 (CPU Scheduling) ...
OS-operating systems- ch05 (CPU Scheduling) ...
 
Improved Approval Flow in Odoo 17 Studio App
Improved Approval Flow in Odoo 17 Studio AppImproved Approval Flow in Odoo 17 Studio App
Improved Approval Flow in Odoo 17 Studio App
 
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
 
How To Create Editable Tree View in Odoo 17
How To Create Editable Tree View in Odoo 17How To Create Editable Tree View in Odoo 17
How To Create Editable Tree View in Odoo 17
 
Basic Civil Engineering notes on Transportation Engineering & Modes of Transport
Basic Civil Engineering notes on Transportation Engineering & Modes of TransportBasic Civil Engineering notes on Transportation Engineering & Modes of Transport
Basic Civil Engineering notes on Transportation Engineering & Modes of Transport
 
How to Manage Website in Odoo 17 Studio App.pptx
How to Manage Website in Odoo 17 Studio App.pptxHow to Manage Website in Odoo 17 Studio App.pptx
How to Manage Website in Odoo 17 Studio App.pptx
 
DEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUM
DEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUMDEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUM
DEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUM
 

#include bitsstdc++.h#include unistd.h#include GLglew.h.pdf

  • 1. #include #include #include #include #include #include #include #include #include #include #include #include #include #include "opencv2/core/opengl.hpp" // #include "t0.cpp" // #include "t1.cpp" using namespace std; using namespace cv; #define STB_IMAGE_IMPLEMENTATION #include "stb_image/stb_image.h" #define STB_IMAGE_WRITE_IMPLEMENTATION #include "stb_image/stb_image_write.h" cv::Mat out; cv::Mat img = cv::imread("fish.png"); void contours_algo() { std::vector> contours; // cv::namedWindow("In",0); // cv::namedWindow("Out",0); std::vector hierarchy; cv::findContours(out,contours,hierarchy,cv::RETR_LIST,cv::CHAIN_APPROX_SIMPLE); cv::Mat mask = cv::Mat::zeros(img.size(),CV_8UC3); cv::drawContours(mask,contours,-1,cv::Scalar(255,255,255),5);
  • 2. contours.push_back(vector()); hierarchy.push_back(cv::Vec4i()); cv::imshow("In",img); cv::imshow("Mask", mask); cv::imshow("Out",out); cv::waitKey(0); } float perpendicularDistance(const Point& p, const Point& lineStart, const Point& lineEnd) { float x = p.x; float y = p.y; float x1 = lineStart.x; float y1 = lineStart.y; float x2 = lineEnd.x; float y2 = lineEnd.y; float A = x - x1; float B = y - y1;
  • 3. float C = x2 - x1; float D = y2 - y1; float dot = A *C + B *D; float len_sq = C * C + D * D; float param = dot / len_sq; float xx, yy; if (param < 0) { xx = x1; yy = y1; } else if (param > 1) { xx = x2; yy = y2;
  • 4. } else { xx = x1 + param * C; yy = y1 + param * D; } float dx = x - xx; float dy = y - yy; return sqrt(dx * dx + dy * dy); } void simplifyContourRDP(const vector& contour, int startIdx, int endIdx, float epsilon, vector& simplifiedContour) { if (endIdx <= startIdx + 1) { // Base case: only two points in the line segment return;
  • 5. } float maxDistance = 0.0; int maxDistanceIdx = 0; Point lineStart = contour[startIdx]; Point lineEnd = contour[endIdx]; // Find the point with the maximum perpendicular distance // from the line segment for (int i = startIdx + 1; i < endIdx; ++i) { float distance = perpendicularDistance(contour[i], lineStart, lineEnd); if (distance > maxDistance) { maxDistance = distance; maxDistanceIdx = i;
  • 6. } } // If the maximum distance is greater than epsilon, // include the point in the simplified contour if (maxDistance > epsilon) { simplifiedContour.push_back(contour[maxDistanceIdx]); // Simplify the left and right sub-contours simplifyContourRDP(contour, startIdx, maxDistanceIdx, epsilon, simplifiedContour); simplifyContourRDP(contour, maxDistanceIdx, endIdx, epsilon, simplifiedContour); } } vector simplifyContourRDP(const vector& contour, float epsilon) {
  • 7. vector simplifiedContour; simplifiedContour.push_back(contour.front()); simplifiedContour.push_back(contour.back()); simplifyContourRDP(contour, 0, contour.size() - 1, epsilon, simplifiedContour); return simplifiedContour; } int main(int argc , char **argv) { cv::Mat bilateralimg,medianimg,out; cv::Mat edges = cv::imread("fish.png", cv::IMREAD_GRAYSCALE); cv::Mat img = cv::imread("fish.png"); cv::Mat img2 = cv::imread("fish.png",cv::IMREAD_GRAYSCALE); Mat grayimg; cvtColor(img, grayimg, cv::COLOR_BGR2GRAY); Mat binaryImage; threshold(grayimg, binaryImage, 128, 255, THRESH_BINARY); vector> contours; findContours(binaryImage, contours, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE);
  • 8. vector> simplifiedContours; float epsilon = 10.0; // Adjust this value to control the level of simplification for (const auto& contour : contours) { vector simplifiedContour = simplifyContourRDP(contour, epsilon); simplifiedContours.push_back(simplifiedContour); } Mat resultImage = img.clone(); drawContours(resultImage, simplifiedContours, -1, Scalar(0, 0, 255), 2); // Display the result imshow("Simplified Contours", resultImage); waitKey(0); return 0;
  • 9. cv::namedWindow("In",0); cv::namedWindow("Out",0); cv::medianBlur(img,medianimg,15); if (img.empty() || img2.empty()) { std::cerr << "Error loading image(s)" << std::endl; return 1; } cv::Canny(img,out,70,300); cv::threshold(img,out,100,255,cv::THRESH_BINARY); cv::namedWindow("In",0); cv::namedWindow("Out",0); contours_algo(); cv::imshow("window",img); cv::imshow("window2GRAY_SCALED",img2); cv::imshow("Median",medianimg); cv::imshow("In",img); cv::imshow("Out",out); cv::waitKey(0); return 0; } In this code I am getting segmentation fault(core dumped) error please fix this.