how to detect concave and convex areas on human skin
extern "C"
{
FUNCTION_ATTRIBUTE
void rough(char *path, char *output) {
cv::Mat image = cv::imread(path, cv::IMREAD_UNCHANGED);
if (image.empty()) {
std::cerr << "Error: Could not load image from path: " << path << std::endl;
return;
}
cv::Mat gray, binary;
if (image.channels() > 1) {
cv::cvtColor(image, gray, cv::COLOR_BGR2GRAY);
} else {
gray = image.clone();
}
cv::threshold(gray, binary, 128, 255, cv::THRESH_BINARY | cv::THRESH_OTSU);
std::vector<std::vector<cv::Point>> contours;
cv::findContours(binary, contours, cv::RETR_LIST, cv::CHAIN_APPROX_SIMPLE);
for (size_t i = 0; i < contours.size(); i++) {
if (contours[i].size() < 3) continue; // Bỏ qua nếu không đủ điểm
std::vector<int> hullIndices;
cv::convexHull(contours[i], hullIndices, false);
if (hullIndices.size() < 4) continue; // Bỏ qua nếu không đủ điểm
std::vector<cv::Vec4i> defects;
cv::convexityDefects(contours[i], hullIndices, defects);
cv::drawContours(image, contours, (int)i, cv::Scalar(0, 255, 0), 2);
if (defects.empty()) continue;
for (const cv::Vec4i &defect : defects) {
int farIdx = defect[2];
if (farIdx < 0 || farIdx >= contours[i].size()) continue;
float depth = defect[3] / 256.0;
if (depth > 10) {
cv::Point farPoint = contours[i][farIdx];
cv::circle(image, farPoint, 5, cv::Scalar(0, 0, 255), -1);
}
}
}
bool success = cv::imwrite(output, image);
if (!success) {
std::cerr << "Error: Failed to save image to path: " << output << std::endl;
}
}
}