gray2rgb&resize

#gray2rgb&resize This is a sample Python script.

# Press Shift+F10 to execute it or replace it with your code.
# Press Double Shift to search everywhere for classes, files, tool windows, actions, and settings.

import os
import cv2

def print_hi(name):
    # Use a breakpoint in the code line below to debug your script.
    print(f'Hi, {name}')  # Press Ctrl+F8 to toggle the breakpoint.

def convertImg(path,num):
    print("happy...")
    for root,dirs,files in os.walk(path):
        for name in files:
            if(name.endswith('.jpg')):
                path_img=os.path.join(root,name)

                print("path_img:    "+path_img)

                img1 = cv2.imread(path_img, 0)
                print(img1)
                print(img1.shape)
                height, width = img1.shape[:2]
                size = (int(width * 0.25), int(height * 0.25))
                img1 = cv2.resize(img1, size, interpolation=cv2.INTER_AREA)
                print(img1.shape)
                img1_rgb = cv2.cvtColor(img1, cv2.COLOR_GRAY2RGB)
                #cv2.imshow('img1_rgb', img1_rgb)
                print(num + name)
                cv2.imwrite(num + name, img1_rgb)
                #name='1/'+name
                #txt.write(name+" "+name+'\n')

# Press the green button in the gutter to run the script.
if __name__ == '__main__':
    print_hi('PyCharm')

    path = 'D:\\road_img\\5'
    #txtName='1.txt'
    #txt = open(txtName, 'w')
    convertImg(path,'./5/')
    #txt.close()


# See PyCharm help at https://www.jetbrains.com/help/pycharm/
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
写出下列代码可以实现什么功能: #Img = cv2.undistort(Img, K, Dist) Img = cv2.resize(Img,(240,180),interpolation=cv2.INTER_AREA) #将opencv读取的图片resize来提高帧率 img = cv2.GaussianBlur(Img, (5, 5), 0) imgHSV = cv2.cvtColor(img, cv2.COLOR_BGR2HSV) # 将BGR图像转为HSV lower = np.array([h_min, s_min, v_min]) upper = np.array([h_max, s_max, v_max]) mask = cv2.inRange(imgHSV, lower, upper) # 创建蒙版 指定颜色上下限 范围内颜色显示 否则过滤 kernel_width = 4 # 调试得到的合适的膨胀腐蚀核大小 kernel_height = 4 # 调试得到的合适的膨胀腐蚀核大小 kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (kernel_width, kernel_height)) mask = cv2.erode(mask, kernel) mask = cv2.dilate(mask, kernel) mask = cv2.dilate(mask, kernel) light_img = mask[:100,:200 ] cv2.imshow("light",light_img) # 输出红绿灯检测结果 Img1 = Img Img = cv2.cvtColor(Img, cv2.COLOR_BGR2RGB) Img2 = Img cropped2 = Img2[70:128, 0:100] h,w,d = cropped2.shape #提取图像的信息 Img = Image.fromarray(Img) Img = ValImgTransform(Img) # 连锁其它变形,变为tesor Img = torch.unsqueeze(Img, dim=0) # 对tesor进行升维 inputImg = Img.float().to(Device) # 让数据能够使用 OutputImg = Unet(inputImg) Output = OutputImg.cpu().numpy()[0] OutputImg = OutputImg.cpu().numpy()[0, 0] OutputImg = (OutputImg * 255).astype(np.uint8) Input = Img.numpy()[0][0] Input = (Normalization(Input) * 255).astype(np.uint8) OutputImg = cv2.resize(OutputImg,(128,128),interpolation=cv2.INTER_AREA) # 将opencv读取的图片resize来提高帧率 ResultImg = cv2.cvtColor(Input, cv2.COLOR_GRAY2RGB) ResultImg[..., 1] = OutputImg cropped = ResultImg[80:128, 20:100] cropped1 = OutputImg[80:128, 20:100] cv2.imshow("out", cropped1)#显示处理后的图像 cv2.imshow("Img2", Img2) cv2.imshow("Img0", cropped)#显示感兴趣区域图像 print(reached)
07-09

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值