import cv2 | Import OpenCV |
img = cv2.imread("image.jpg") | Read image (BGR) |
cv2.imread("image.jpg", cv2.IMREAD_GRAYSCALE) | Read as grayscale |
cv2.imwrite("output.jpg", img) | Save image |
cv2.imshow("Window", img) | Display image |
cv2.waitKey(0) | Wait for key press |
cv2.destroyAllWindows() | Close windows |
img.shape | (height, width, channels) |
img.dtype | Data type |
img.size | Total pixels |
img[y, x] | Access pixel (BGR) |
img[y1:y2, x1:x2] | ROI (Region of Interest) |
b, g, r = cv2.split(img) | Split channels |
cv2.merge([b, g, r]) | Merge channels |
cv2.cvtColor(img, cv2.COLOR_BGR2RGB) | BGR to RGB |
cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) | BGR to Grayscale |
cv2.cvtColor(img, cv2.COLOR_BGR2HSV) | BGR to HSV |
cv2.cvtColor(img, cv2.COLOR_BGR2LAB) | BGR to LAB |
cv2.cvtColor(img, cv2.COLOR_GRAY2BGR) | Grayscale to BGR |
cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY) | Binary threshold |
cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU) | Otsu's threshold |
cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 11, 2) | Adaptive threshold |
cv2.inRange(hsv, lower, upper) | Color range mask |
cv2.resize(img, (width, height)) | Resize image |
cv2.resize(img, None, fx=0.5, fy=0.5) | Scale by factor |
cv2.rotate(img, cv2.ROTATE_90_CLOCKWISE) | Rotate 90° |
cv2.flip(img, 1) | Horizontal flip |
cv2.flip(img, 0) | Vertical flip |
cv2.warpAffine(img, M, (w, h)) | Affine transform |
cv2.warpPerspective(img, M, (w, h)) | Perspective transform |
cv2.getRotationMatrix2D(center, angle, scale) | Rotation matrix |
cv2.getAffineTransform(pts1, pts2) | Affine matrix |
cv2.getPerspectiveTransform(pts1, pts2) | Perspective matrix |
cv2.blur(img, (5, 5)) | Average blur |
cv2.GaussianBlur(img, (5, 5), 0) | Gaussian blur |
cv2.medianBlur(img, 5) | Median blur |
cv2.bilateralFilter(img, 9, 75, 75) | Bilateral filter |
cv2.Canny(img, 100, 200) | Canny edge detection |
cv2.Sobel(img, cv2.CV_64F, 1, 0) | Sobel X |
cv2.Sobel(img, cv2.CV_64F, 0, 1) | Sobel Y |
cv2.Laplacian(img, cv2.CV_64F) | Laplacian |
kernel = np.ones((5,5), np.uint8) | Create kernel |
cv2.erode(img, kernel, iterations=1) | Erosion |
cv2.dilate(img, kernel, iterations=1) | Dilation |
cv2.morphologyEx(img, cv2.MORPH_OPEN, kernel) | Opening (erosion + dilation) |
cv2.morphologyEx(img, cv2.MORPH_CLOSE, kernel) | Closing (dilation + erosion) |
cv2.morphologyEx(img, cv2.MORPH_GRADIENT, kernel) | Morphological gradient |
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) | Find contours |
cv2.drawContours(img, contours, -1, (0,255,0), 2) | Draw all contours |
cv2.contourArea(cnt) | Contour area |
cv2.arcLength(cnt, True) | Contour perimeter |
cv2.approxPolyDP(cnt, epsilon, True) | Approximate polygon |
cv2.boundingRect(cnt) | Bounding rectangle |
cv2.line(img, pt1, pt2, color, thickness) | Draw line |
cv2.rectangle(img, pt1, pt2, color, thickness) | Draw rectangle |
cv2.circle(img, center, radius, color, thickness) | Draw circle |
cv2.ellipse(img, center, axes, angle, 0, 360, color) | Draw ellipse |
cv2.polylines(img, [pts], True, color, thickness) | Draw polygon |
cv2.putText(img, text, org, font, scale, color, thickness) | Draw text |
orb = cv2.ORB_create() | Create ORB detector |
kp, des = orb.detectAndCompute(img, None) | Detect and compute |
cv2.drawKeypoints(img, kp, None) | Draw keypoints |
sift = cv2.SIFT_create() | Create SIFT |
bf = cv2.BFMatcher() | Brute-force matcher |
matches = bf.match(des1, des2) | Match descriptors |
cv2.HoughLines(edges, 1, np.pi/180, 200) | Hough lines |
cv2.HoughLinesP(edges, 1, np.pi/180, 100, minLineLength, maxLineGap) | Probabilistic Hough lines |
cv2.HoughCircles(gray, cv2.HOUGH_GRADIENT, 1, 20) | Hough circles |
cap = cv2.VideoCapture(0) | Open webcam |
cap = cv2.VideoCapture("video.mp4") | Open video file |
ret, frame = cap.read() | Read frame |
cap.get(cv2.CAP_PROP_FPS) | Get FPS |
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 640) | Set width |
cap.release() | Release capture |
fourcc = cv2.VideoWriter_fourcc(*'XVID') | Video codec |
out = cv2.VideoWriter('output.avi', fourcc, 20.0, (640,480)) | Create writer |
out.write(frame) | Write frame |
out.release() | Release writer |