OpenCV_之sort_box

比如在缺陷检测中,一般情况下会对检测目标进行排序,以边更好的进行操作;
下面贴上相关的代码以及效果图:

void sort_box(vector<Rect> &boxes)
{
	int size = boxes.size();
	for(int t = 0; t < size - 1; t++)
	{
		for(int j = i; j < size; j++)
		{
			int x = boxes[j].x;
			int y = boxes[j].y;
			if(y < boxes[i].y)
			{
				Rect temp = boxes[i];
				boxes[i] = boxes[j];
				boxes[j] = temp;
			}
		}
	}
}
以下是使用Opencv进行手写数字识别的代码示例: ``` import cv2 import numpy as np def get_contour_precedence(contour, cols): tolerance_factor = 10 origin = cv2.boundingRect(contour) return ((origin[1]//tolerance_factor)*tolerance_factor)*cols + origin[0] def main(): # Load the model net = cv2.dnn.readNet("model.pb") classes = [] with open("model.txt", "r") as f: classes = [line.strip() for line in f.readlines()] # Capture from the default camera cap = cv2.VideoCapture(0) while True: _, frame = cap.read() # Convert the captured frame into grayscale gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) # Apply adaptive thresholding to segment the foreground from the background thresh = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY_INV, 9, 2) # Find contours in the binary image contours, _ = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) # Sort the contours in the order of y-coordinate contours.sort(key=lambda x:get_contour_precedence(x, frame.shape[1])) for contour in contours: # Get the bounding box for the contour x, y, w, h = cv2.boundingRect(contour) # Extract the digit from the binary image digit_roi = thresh[y:y+h, x:x+w] # Resize the digit image to (28, 28) digit_roi = cv2.resize(digit_roi, (28, 28)) # Expand the image to (32, 32) for consistency with the training data digit_roi = cv2.copyMakeBorder(digit_roi, 2, 2, 2, 2, cv2.BORDER_CONSTANT, value=0) # Normalize the pixel values to the range (0, 1) digit_roi = digit_roi.astype("float32") / 255.0 # Add an extra dimension to the image to make it compatible with the input shape of the model (1, 32, 32, 1) digit_roi = np.expand_dims(digit_roi, axis=-1) digit_roi = np.expand_dims(digit_roi, axis=0) # Feed the image to the model and get the predicted class label net.setInput(digit_roi) preds = net.forward() class_idx = np.argmax(preds) # Convert the class label to the corresponding digit digit = classes[class_idx] # Draw the digit and the bounding box on the original frame cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2) cv2.putText(frame, digit, (x+int(w/4), y+int(h/2)), cv2.FONT_HERSHEY_COMPLEX, 1, (0, 255, 0), 2) # Show the output cv2.imshow("Handwritten Digit Recognition", frame) # Press 'q' to quit if cv2.waitKey(1) == ord('q'): break cap.release() cv2.destroyAllWindows() if __name__ == "__main__": main() ``` 这段代码使用Opencv实现了从摄像头捕获图像,然后使用预训练好的模型对手写数字进行识别,并在屏幕上展示预测结果。在使用时,请确保正确设置模型的路径和模型的输入输出形状。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值