| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 1 | 2 | 3 | ||||
| 4 | 5 | 6 | 7 | 8 | 9 | 10 |
| 11 | 12 | 13 | 14 | 15 | 16 | 17 |
| 18 | 19 | 20 | 21 | 22 | 23 | 24 |
| 25 | 26 | 27 | 28 | 29 | 30 | 31 |
- edge
- Contour
- Android
- Binary
- UNO
- parameter
- Class
- c++
- Filtering
- sensor
- atmega328
- public
- SERIAL
- flutter
- aduino
- wpf
- Pointer
- Gaussian
- file access
- subpixel
- digitalRead
- Gradient
- compare
- Encapusulation
- APP
- Unity
- memory
- Read
- mfc
- stream
- Today
- Total
폴크(FOLC)
치수 계산하기 - subpixel - edge - direction 2 본문
1D gradient 기반 edge 검출에 이어, parabola 보간을 이용한 subpixel 정밀도 위치 추정, 그리고 gradient 방향(양/음)을 통한 edge 방향성 분석까지 포함하는 정밀한 CD 측정 알고리즘
예제
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace std;
using namespace cv;
enum class EdgeDirection { POSITIVE, NEGATIVE };
struct EdgePoint {
float position; // subpixel 위치
EdgeDirection direction; // gradient 방향
};
// 파라볼라 보간으로 subpixel 위치 추정
float parabolaSubpixelOffset(float g_prev, float g_curr, float g_next) {
float denom = g_prev - 2 * g_curr + g_next;
if (fabs(denom) < 1e-5) return 0.0f; // 안정성 처리
return 0.5f * (g_prev - g_next) / denom;
}
// 1D gradient + 방향 분석
vector<EdgePoint> detectEdges1D(const vector<float>& profile) {
vector<EdgePoint> edges;
for (int i = 1; i < profile.size() - 1; ++i) {
float g_prev = profile[i - 1];
float g_curr = profile[i];
float g_next = profile[i + 1];
float gradient = 0.5f * (g_next - g_prev);
// 극대/극소 점인지 확인 (edge 후보)
if ((g_curr > g_prev && g_curr > g_next) || (g_curr < g_prev && g_curr < g_next)) {
float offset = parabolaSubpixelOffset(g_prev, g_curr, g_next);
EdgeDirection dir = (gradient > 0) ? EdgeDirection::POSITIVE : EdgeDirection::NEGATIVE;
edges.push_back({i + offset, dir});
}
}
return edges;
}
void run1DAnalysis(const Mat& gray, int rowIdx) {
// intensity 프로파일 추출
vector<float> profile(gray.cols);
for (int x = 0; x < gray.cols; ++x)
profile[x] = static_cast<float>(gray.at<uchar>(rowIdx, x));
// 1D gradient (미분)
vector<float> grad(profile.size());
for (int i = 1; i < profile.size() - 1; ++i)
grad[i] = 0.5f * (profile[i + 1] - profile[i - 1]);
// edge 추출
vector<EdgePoint> edges = detectEdges1D(grad);
// 출력
for (const auto& pt : edges) {
string d = (pt.direction == EdgeDirection::POSITIVE) ? "↑ (Dark→Bright)" : "↓ (Bright→Dark)";
cout << "Edge at x = " << pt.position << " : " << d << endl;
}
// CD 측정 (예시: 첫 + edge, 첫 - edge 간 거리)
float cd = -1.0f;
int firstPos = -1, firstNeg = -1;
for (int i = 0; i < edges.size(); ++i) {
if (firstPos < 0 && edges[i].direction == EdgeDirection::POSITIVE)
firstPos = i;
else if (firstNeg < 0 && edges[i].direction == EdgeDirection::NEGATIVE)
firstNeg = i;
}
if (firstPos >= 0 && firstNeg >= 0) {
cd = fabs(edges[firstNeg].position - edges[firstPos].position);
cout << "CD = " << cd << " pixels" << endl;
}
// 시각화
Mat vis;
cvtColor(gray, vis, COLOR_GRAY2BGR);
for (const auto& pt : edges) {
Scalar color = (pt.direction == EdgeDirection::POSITIVE) ? Scalar(0,255,0) : Scalar(0,0,255);
circle(vis, Point((int)pt.position, rowIdx), 2, color, -1);
}
imshow("1D Edge Detection (Subpixel)", vis);
waitKey(0);
}
int main(int argc, char** argv) {
if (argc < 2) {
cout << "Usage: ./Edge1DSubpixel <image_path>" << endl;
return -1;
}
Mat img = imread(argv[1], IMREAD_GRAYSCALE);
if (img.empty()) {
cerr << "Image load failed!" << endl;
return -1;
}
int rowToAnalyze = img.rows / 2; // 분석할 row 선택
run1DAnalysis(img, rowToAnalyze);
return 0;
}