出错 rotated2 (line 6) img_rotated = imrotate(img, 30);
时间: 2024-03-18 10:44:26 浏览: 25
这个错误通常是由于 MATLAB 中 imrotate 函数的输入参数不正确引起的。请确认您的输入参数是否正确。imrotate 函数有两个输入参数,第一个参数是要旋转的图像,第二个参数是旋转角度。请确保您的图像是一个正确的图像文件,并且旋转角度是一个数字。此外,您可以尝试将图像转换为灰度图像,然后再旋转。
相关问题
def rotate_batch_image(batch_image, angle): rotateds = [] for img in batch_image: img = img * 0.5 + 0.5 pil_img = tf.to_pil_image(img.type(torch.FloatTensor)) img_rotated = tf.rotate(pil_img, angle=angle) # Image._show(img_rotated) img_rotated = (tf.to_tensor(img_rotated) - 0.5) / 0.5 rotateds.append(img_rotated) rotateds = torch.stack(rotateds, dim=0).cuda() return rotateds
这是一个用于旋转批量图像的Python函数,输入为一个批量图像数据(batch_image)和旋转角度(angle),输出为旋转后的批量图像数据(rotateds)。
具体实现如下:
1. 遍历批量图像数据中的每张图像。
2. 将每张图像中的像素值从[-1, 1]的范围转换到[0, 1]的范围。
3. 将每张图像转换成PIL的图像格式。
4. 使用PIL库的rotate函数对每张图像进行旋转,并指定旋转角度。
5. 将旋转后的图像再次转换成Tensor格式,并将像素值转换到[-1, 1]的范围。
6. 将旋转后的所有图像数据存储在一个列表中。
7. 使用torch.stack函数将所有旋转后的图像数据拼接成一个张量,并将其移动到GPU上。
8. 返回旋转后的批量图像数据。
def process_image(image_path, output_folder): gray = cv2.imread(image_path) edges = cv2.Canny(gray, 50, 150) lines = cv2.HoughLines(edges, 1, np.pi / 180, threshold=90) sum_angle = 0 count = 0 if lines is not None: for line in lines: rho, theta = line[0] angle = theta * 180 / np.pi sum_angle += angle count += 1 avg_angle = sum_angle / count rotated_image = rotate_image(gray, avg_angle) filename = os.path.basename(image_path) output_path = os.path.join(output_folder, filename) cv2.imwrite(output_path, rotated_image) def rotate_image(image, angle): height, width = image.shape[:2] center = (width // 2, height // 2) rotation_matrix = cv2.getRotationMatrix2D(center, angle, 1.0) rotated_image = cv2.warpAffine(image, rotation_matrix, (width, height)) return rotated_image input_folder = 'img_out_binary' output_folder = 'rotated_img' os.makedirs(output_folder, exist_ok=True) for filename in os.listdir(input_folder): if filename.endswith(".jpg") or filename.endswith(".JPG"): image_path = os.path.join(input_folder, filename) process_image(image_path, output_folder) 优化程序,使程序可以对图片文件名包含汉字的图片进行处理和保存
要使程序能够处理和保存包含汉字的图片文件名,您可以对`process_image()`函数进行修改。在处理图片之前,您可以对文件名进行URL编码,然后使用编码后的文件名来保存旋转后的图片。以下是修改后的代码:
```python
def process_image(image_path, output_folder):
# 对文件名进行URL编码
encoded_filename = urllib.parse.quote(os.path.basename(image_path))
gray = cv2.imread(image_path)
edges = cv2.Canny(gray, 50, 150)
lines = cv2.HoughLines(edges, 1, np.pi / 180, threshold=90)
sum_angle = 0
count = 0
if lines is not None:
for line in lines:
rho, theta = line[0]
angle = theta * 180 / np.pi
sum_angle += angle
count += 1
avg_angle = sum_angle / count
rotated_image = rotate_image(gray, avg_angle)
# 使用编码后的文件名来保存旋转后的图片
output_path = os.path.join(output_folder, encoded_filename)
cv2.imwrite(output_path, rotated_image)
```
在修改后的代码中,首先使用`urllib.parse.quote()`函数对文件名进行编码,然后使用编码后的文件名来构建输出路径。这样可以确保保存的文件名不会受到包含汉字的影响。
另外,确保在程序中正确导入`urllib.parse`模块来使用URL编码函数。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)