Files
2nd/10_Wiki/Topics/AI_and_ML/Shape-Feature-Extraction.md
T
Antigravity Agent f8b21af4be Wiki cleanup: error-doc removal, dedup merge, link normalization
10_Wiki/Topics 대규모 정리:
- 오류 캡처/미완성 stub 문서 227개 제거
- 교차폴더 중복 43클러스터 병합 (63파일 → redirect)
- 링크명 정규화: 깨진 링크 수정·redirect 직결·개념 매핑 ~2,400건
- 카테고리 MOC 6개 신규 생성
- Graph 섹션 미해결 related-keyword 링크 10,058건 제거

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-20 23:52:15 +09:00

161 lines
5.1 KiB
Markdown

---
id: wiki-2026-0508-shape-feature-extraction
title: Shape Feature Extraction
category: 10_Wiki/Topics
status: verified
canonical_id: self
aliases: [Shape Descriptors, HOG, SIFT, Contour Features]
duplicate_of: none
source_trust_level: A
confidence_score: 0.9
verification_status: applied
tags: [computer-vision, feature-extraction, image-processing]
raw_sources: []
last_reinforced: 2026-05-10
github_commit: pending
tech_stack:
language: python
framework: OpenCV / scikit-image / PyTorch
---
# Shape Feature Extraction
## 매 한 줄
> **"매 image / object 에서 numerical descriptor 뽑기 — boundary, region, gradient"**. 매 classical (HOG, SIFT, Hu moments, Fourier descriptors) 부터 매 deep features (CNN backbone, DINOv2/v3, SAM2 mask embedding) 까지의 spectrum. 매 2026 default: deep features for recognition, classical for low-data / explainable / edge.
## 매 핵심
### 매 분류
- **Boundary-based**: contour chain code, Fourier descriptors, polygon approx.
- **Region-based**: area, perimeter, eccentricity, Hu moments (rotation/scale invariant).
- **Gradient-based**: HOG (Dalal 2005), SIFT (Lowe 2004), SURF, ORB.
- **Texture+shape**: LBP, GLCM.
- **Deep**: CNN penultimate layer, ViT [CLS] token, DINOv3 patch features.
### 매 Invariance 요구
- Translation: 매 거의 모든 method.
- Rotation: Hu moments, SIFT, RIFT.
- Scale: SIFT, multi-scale CNN.
- Illumination: HOG (gradient), normalized embeddings.
- Affine: ASIFT.
### 매 응용
1. Object recognition (legacy + edge).
2. Image retrieval / re-id (deep embeddings).
3. OCR pre-processing (contour).
4. Medical imaging (lesion shape descriptors).
5. Industrial defect inspection.
6. Robot grasp planning (object silhouette).
## 💻 패턴
### Contour features (OpenCV)
```python
import cv2, numpy as np
gray = cv2.imread("obj.png", 0)
_, bw = cv2.threshold(gray, 0, 255, cv2.THRESH_OTSU)
contours, _ = cv2.findContours(bw, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
c = max(contours, key=cv2.contourArea)
area = cv2.contourArea(c)
peri = cv2.arcLength(c, True)
circ = 4 * np.pi * area / (peri ** 2)
hu = cv2.HuMoments(cv2.moments(c)).flatten()
```
### HOG
```python
from skimage.feature import hog
feat, vis = hog(gray, orientations=9, pixels_per_cell=(8,8),
cells_per_block=(2,2), visualize=True)
```
### SIFT (OpenCV)
```python
sift = cv2.SIFT_create()
kp, desc = sift.detectAndCompute(gray, None) # desc: (N, 128)
```
### Fourier descriptors
```python
def fourier_descriptors(contour, k=20):
pts = contour[:, 0, 0] + 1j * contour[:, 0, 1]
fd = np.fft.fft(pts)
fd[0] = 0 # translation invariant
fd /= np.abs(fd[1]) # scale invariant
return np.abs(fd[1:k+1]) # rotation invariant (magnitude)
```
### Deep feature (DINOv3)
```python
import torch
from transformers import AutoModel, AutoImageProcessor
proc = AutoImageProcessor.from_pretrained("facebook/dinov3-base")
model = AutoModel.from_pretrained("facebook/dinov3-base").eval().cuda()
inp = proc(img, return_tensors="pt").to("cuda")
with torch.no_grad():
feats = model(**inp).last_hidden_state # (1, N+1, D)
cls_emb = feats[:, 0] # global shape/appearance
```
### SAM2 mask + descriptor pipeline
```python
from sam2.build_sam import build_sam2
from sam2.sam2_image_predictor import SAM2ImagePredictor
sam = build_sam2("sam2_hiera_l.yaml", "sam2_l.pt")
pred = SAM2ImagePredictor(sam)
pred.set_image(img)
masks, _, _ = pred.predict(point_coords=pts, point_labels=lbl)
# 매 mask 내부 영역만 dino feature 뽑기 → object-centric descriptor
```
### Image retrieval pipeline
```python
emb = []
for p in paths:
e = dino_embed(load(p))
emb.append(e / e.norm())
emb = torch.stack(emb)
# query
q = dino_embed(load(query))
q /= q.norm()
sims = (emb @ q.T).flatten()
topk = sims.topk(10).indices
```
## 매 결정 기준
| 상황 | Approach |
|---|---|
| Modern recognition / retrieval | DINOv3 / CLIP embedding |
| Explainable / regulatory | Hu moments, contour |
| Real-time embedded | ORB or tiny CNN |
| Robust to occlusion | local features (SIFT/SuperPoint) |
| Mask 필요 + descriptor | SAM2 + DINO |
**기본값**: DINOv3 embedding for general purpose.
## 🔗 Graph
- 부모: [[Computer Vision|Computer-Vision]] · [[Feature-Extraction]]
- 변형: [[HOG]] · [[SIFT]]
- 응용: [[OCR]]
- Adjacent: [[Image-Segmentation]] · [[CLIP]]
## 🤖 LLM 활용
**언제**: dataset 작거나 explainability 요구 → classical. Otherwise deep.
**언제 X**: 매 generic image classification — end-to-end deep model 가 매 simpler.
## ❌ 안티패턴
- **HOG + SVM in 2026**: deep baseline 보다 명확히 약함 unless tiny data.
- **Hand-crafted features then deep classifier**: 매 mismatch — pick one paradigm.
- **No normalization**: scale/illumination drift → 매 retrieval 실패.
- **SIFT 특허 우려**: 2020+ 매 expired, 그래도 license 확인.
## 🧪 검증 / 중복
- Verified (Lowe 2004 SIFT, Dalal 2005 HOG, OpenCV docs, DINOv3 paper).
- 신뢰도 A.
## 🕓 Changelog
| 날짜 | 변경 |
|---|---|
| 2026-05-08 | Phase 1 |
| 2026-05-10 | Manual cleanup — classical + DINOv3/SAM2 2026 |