large_img1 = cv2.resize(img3, (0, 0), fx=2self.scale, fy=2self.scale) 改为以某个点为基准放大图片
时间: 2024-02-09 09:08:06 浏览: 116
以下是一个实现以某个点为基准放大图片的代码示例:
```
import cv2
import numpy as np
# 读取原图
img = cv2.imread('test.jpg')
# 缩放比例
scale = 2
# 基准点
x, y = 300, 200
# 计算缩放后的图片大小
h, w = img.shape[:2]
new_h, new_w = int(h * scale), int(w * scale)
# 计算变换矩阵
M = np.float32([[scale, 0, (1 - scale) * x], [0, scale, (1 - scale) * y]])
# 对图像进行仿射变换
large_img1 = cv2.warpAffine(img, M, (new_w, new_h))
# 显示结果
cv2.imshow('large_img1', large_img1)
cv2.waitKey(0)
cv2.destroyAllWindows()
```
这段代码首先读取原图,然后指定了缩放比例和基准点的坐标。接着计算出缩放后的图片大小,并根据基准点和缩放比例计算出变换矩阵。最后使用 `cv2.warpAffine` 函数对图像进行仿射变换,得到以该点为基准放大后的图片。
相关问题
def locate_carPlate(car_pic, resize_rate=1): # 预处理图像 img = car_pic pic_hight, pic_width = img.shape[:2] if pic_width > MAX_WIDTH: pic_rate = MAX_WIDTH / pic_width img = cv2.resize(img, (MAX_WIDTH, int(pic_hight * pic_rate)), interpolation=cv2.INTER_LANCZOS4) # cv2.imshow("img", img) if resize_rate != 1: img = cv2.resize(img, (int(pic_width * resize_rate), int(pic_hight * resize_rate)), interpolation=cv2.INTER_LANCZOS4) pic_hight, pic_width = img.shape[:2] blur = cfg["blur"] if blur > 0: img = cv2.GaussianBlur(img, (blur, blur), 0) # 图片分辨率调整 oldimg = img img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # 将BGR格式转换成灰度图片 # cv2.imshow("gray", img) kernel = np.ones((20, 20), np.uint8) img_opening = cv2.morphologyEx(img, cv2.MORPH_OPEN, kernel) # 开运算 # cv2.imshow("opening", img_opening) img_opening = cv2.addWeighted(img, 1, img_opening, -1, 0) # 图像叠加,img - img_opening # cv2.imshow("opening", img_opening) ret, img_thresh = cv2.threshold(img_opening, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU) # 阈值处理 # cv2.imshow("tresh", img_thresh) img_edge = cv2.Canny(img_thresh, 100, 200) # 边缘检测 # cv2.imshow("edge", img_edge) kernel = np.ones((cfg["morphologyr"], cfg["morphologyc"]), np.uint8) img_edge1 = cv2.morphologyEx(img_edge, cv2.MORPH_CLOSE, kernel) # 闭运算 # cv2.imshow("edge1", img_edge1) img_edge2 = cv2.morphologyEx(img_edge1, cv2.MORPH_OPEN, kernel) # cv2.imshow("edge2", img_edge2)
这段代码是用来定位车牌的,首先对图片进行预处理,包括对图片进行缩放、高斯模糊、灰度化、开运算、阈值处理、边缘检测和闭运算等操作,最终得到一个可以用来定位车牌的图像。其中,MAX_WIDTH是一个常量,表示图片的最大宽度;blur表示高斯模糊的半径大小;morphologyr和morphologyc是闭运算和开运算的卷积核大小。这段代码是车牌识别算法中比较重要的部分,通过这些处理可以将车牌从原始图片中准确地定位出来。
程序无法执行,修改class Processor(): def __init__(self): self._inspect_step = int(cfg.get('PROCESS', 'INSPECT_STEP')) def capture_img(self): global aco aco = aco + 1 self._cam.stream_on() raw_image = self._cam.data_stream[0].get_image() if raw_image is None: print("Getting image failed.\n") return None print("Frame ID: {} Height: {} Width: {} Count: {}\n" .format(raw_image.get_frame_id(), raw_image.get_height(), raw_image.get_width(), aco - 2)) numpy_image = raw_image.get_numpy_array() if numpy_image is None: return None img = Image.fromarray(numpy_image, 'L') if self._issave: picfile = '{}/{}.bmp'.format(self._picpath, self._piccount) self._piccount = self._piccount + 1 img.save(picfile) if self._isshow: w, h = img.size scale = min(1.0 * IMG_RESIZE_W / w, 1.0 * IMG_RESIZE_H / h) self._img = ImageTk.PhotoImage(image=img.resize((int(w * scale), int(h * scale)), Image.ANTIALIAS)) self._show_cb(self._img) self._cam.stream_off() return img def process_img(self, img): return PROC_OK def inspect(self): print("process2") time.sleep(1) def rotate(self): count = 0 aco = 0 self.threadsignal = 0 while self._running: v = self._dgl.qpin(ROT) if len(v) > 0: count = count + sum(v) if count > self._inspect_step: aco += 1 img = self.capture_img() count = count - self._inspect_step if __name__ == '__main__': task2 = multiprocessing.Process(target=self.inspect) task2.start() task1 = multiprocessing.Process(target=self.rotate) task1.start()
在这段代码中,有几个问题可以被改进。首先,类 `Processor` 中的全局变量 `aco` 可能会导致并发问题,在多进程环境下应该避免使用全局变量。其次,代码中出现了 `time.sleep()`,这会阻塞当前进程并造成性能瓶颈,应该尽量避免使用。
以下是可能的改进方案:
1. 将 `aco` 变量改为实例变量,避免多进程之间的并发问题。
2. 使用 `Queue` 或其他进程间通信机制替代全局变量,避免多进程之间的竞争。
3. 将 `time.sleep()` 改为使用定时器或者事件,避免阻塞进程并提高程序的性能。
4. 在多进程环境中,使用 `if __name__ == '__main__':` 来避免创建子进程时出现递归调用的问题。
以下是修改后的代码示例:
```python
import multiprocessing
from queue import Queue
import time
class Processor():
def __init__(self):
self._inspect_step = int(cfg.get('PROCESS', 'INSPECT_STEP'))
self._aco = 0
self._queue = Queue()
def capture_img(self):
self._aco += 1
self._cam.stream_on()
raw_image = self._cam.data_stream[0].get_image()
if raw_image is None:
print("Getting image failed.\n")
return None
print("Frame ID: {} Height: {} Width: {} Count: {}\n".format(raw_image.get_frame_id(), raw_image.get_height(), raw_image.get_width(), self._aco - 2))
numpy_image = raw_image.get_numpy_array()
if numpy_image is None:
return None
img = Image.fromarray(numpy_image, 'L')
if self._issave:
picfile = '{}/{}.bmp'.format(self._picpath, self._piccount)
self._piccount = self._piccount + 1
img.save(picfile)
if self._isshow:
w, h = img.size
scale = min(1.0 * IMG_RESIZE_W / w, 1.0 * IMG_RESIZE_H / h)
self._img = ImageTk.PhotoImage(image=img.resize((int(w * scale), int(h * scale)), Image.ANTIALIAS))
self._show_cb(self._img)
self._cam.stream_off()
return img
def process_img(self, img):
return PROC_OK
def inspect(self):
print("process2")
while self._running:
time.sleep(1)
self._queue.put("task")
def rotate(self):
count = 0
while self._running:
v = self._dgl.qpin(ROT)
if len(v) > 0:
count = count + sum(v)
if count > self._inspect_step:
self._aco += 1
img = self.capture_img()
count = count - self._inspect_step
try:
task = self._queue.get(block=False)
if task == "task":
print("processing task")
except:
pass
if __name__ == '__main__':
p = Processor()
p._running = True
task2 = multiprocessing.Process(target=p.inspect)
task2.start()
task1 = multiprocessing.Process(target=p.rotate)
task1.start()
```
请注意,这里只是对原始代码的一些修改建议,并不能保证这样的修改能够完全解决问题。在实际环境中,还需要根据具体情况进行调整和优化。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231044736.png)
![-](https://img-home.csdnimg.cn/images/20241231045021.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)