如何解决此问题[SpyderKernelApp]警告|无此通信:

2024-03-28 18:00:36 发布

您现在位置:Python中文网/ 问答频道 /正文

我刚开始了解OpenSV,并尝试在调整图像大小后使其不失真。这是我的代码:

import numpy as np
import cv2
# upload the file as a color image and as a gray image
img = cv2.imread('C:/Users/HomePC/Desktop/1.jpg')
img_gray = cv2.imread('C:/Users/HomePC/Desktop/1.jpg',0)

# display images in a window
cv2.imshow('Window',img)
# all windows are closed after pressing any key
cv2.waitKey (0)
cv2.destroyAllWindows()

# save images to a file
image = cv2.imread('C:/Users/HomePC/Desktop/1.jpg')
cv2.imwrite('C:/Users/HomePC/Desktop/1.png',image)

img = cv2.imread ('C:/Users/HomePC/Desktop/1.jpg')
img = cv2.imread ('C:/Users/HomePC/Desktop/1.jpg',1)
(h, w, d) = image.shape#
print("width ={}, height ={}, depth ={}". format (w, h, d))

# access to individual pixels
# access to RGB (!) pixels with coordinates x = 50, y = 100
(B, G, R) = image[100 , 50]
print("R={}, G={}, B={}". format (R, G, B))

# slice arrays and cut out images
# extract a 100x100 pixel square ROI ( Region of Interest ) from the
# input image starting at x=320 ,y=60 at ending at x=420 ,y= 160
roi = image[560:660,320:420]
cv2.imshow("ROI ", roi)
cv2.waitKey(0)

# resize images
# resize the image to 200x200px , ignoring aspect ratio
resized = cv2.resize(image, (200 , 200 ))
cv2.imshow("Fixed Resizing ", resized)
cv2.waitKey(0)

img = cv2.imread('C:/Users/HomePC/Desktop/1.jpg')
h,w= img.shape[0:2] # height and width of the original image
h_new = 500 # the height of the new image
ratio =w/h # the ratio of width to height
w_new = int( h_new * ratio) # width of the new image
resized = cv2.resize(img, (w_new, h_new)) # new image, in the cottage width in the first place!
# although the shape function has a height as the first argument
print(resized.shape)
cv2.imshow('1',resized)

当我尝试在调整大小后使图像不失真时,爬行器会出现以下错误:

内核死机,正在重新启动

[SpyderKernelApp]警告|无此类通信:A45C4FB203FC11EBAED2183BF76912A

它发生在添加这段代码之后:

img = cv2.imread('C:/Users/HomePC/Desktop/1.jpg')
h,w= img.shape[0:2] # height and width of the original image
h_new = 500 # the height of the new image
ratio =w/h # the ratio of width to height
w_new = int( h_new * ratio) # width of the new image
resized = cv2.resize(img, (w_new, h_new)) # new image, in the cottage width in the first place!
# although the shape function has a height as the first argument
print(resized.shape)
cv2.imshow('1',resized)

Tags: oftheimageimgnewwidthcv2users