博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Python OpenCV实例:基于等间隔提取图像缩放和基于区域子块提取图像缩放
阅读量:6136 次
发布时间:2019-06-21

本文共 2126 字,大约阅读时间需要 7 分钟。

  hot3.png

#coding:utf-8'''图像缩放'''import numpy as npimport cv2image = cv2.imread('datas/l1.jpg')# 基于等隔提取图像缩放def scale_simple(image,kx,ky):    # 计算缩放后图像的分辨率    rows = int(np.round(np.abs(image.shape[0] * kx)))    cols = int(np.round(np.abs(image.shape[1] * ky)))        dist = None    if len(image.shape) == 3 and image.shape[2] >= 3:        dist = np.zeros((rows,cols,image.shape[2]),image.dtype)    else:        dist = np.zeros((rows,cols),image.dtype)    for y in range(rows):        for x in range(cols):            new_y =  int((y + 1) / ky + 0.5) - 1            new_x = int((x + 1) / kx + 0.5) - 1            dist[y,x] = image[new_y,new_x]    return dist# 基于区域子块提取图像缩放def area_average(image,left_point,right_point):    temp1,temp2,temp3 = 0,0,0    # 计算区域子块像素点个数    #npix = (right_point['x'] - left_point['x'] + 1) * (right_point['y'] - left_point['y'] + 1)    # 对区域子块各个通道对像素值求和    for i in range(right_point['x'] - left_point['x']):        i += left_point['x']        for j in range(right_point['y'] - left_point['y']):                        j += right_point['y']            temp1 += image[i,j,0]            temp2 += image[i,j,1]            temp3 += image[i,j,2]    vec3b = [temp1,temp2,temp3]    return vec3bdef scale_area(image, kx,ky):    rows = int(np.round(np.abs(image.shape[0] * kx)))    cols = int(np.round(np.abs(image.shape[1] * ky)))    dist = np.zeros((rows,cols,image.shape[2]),image.dtype)    left_row_coord = 0    left_col_coord = 0    for i in range(rows):        x = int((i + 1) / kx + 0.5) - 1        for j in range(cols):            y = int((j + 1) / ky + 0.5) - 1            left_point = {'x':left_row_coord,'y':left_col_coord}            right_point = {'x':x,'y':y}            pixel = area_average(image,left_point,right_point)            dist[i,j] = pixel            left_col_coord = y + 1            left_col_coord = 0        left_row_coord = x + 1        return distresult = scale_simple(image,0.5,0.5)result2 = scale_area(image,0.5,0.5)cv2.imshow('src',image)cv2.imshow('scale-simple',result)cv2.imshow('scale-area',result2)cv2.waitKey()cv2.destroyAllWindows()

203507_XLhO_106657.png

 

转载于:https://my.oschina.net/wujux/blog/1797483

你可能感兴趣的文章
IIS7如何显示详细错误信息
查看>>
Android打包常见错误之Export aborted because fatal lint errors were found
查看>>
Tar打包、压缩与解压缩到指定目录的方法
查看>>
配置spring上下文
查看>>
Python异步IO --- 轻松管理10k+并发连接
查看>>
Oracle中drop user和drop user cascade的区别
查看>>
登记申请汇总
查看>>
Office WORD如何取消开始工作右侧栏
查看>>
Android Jni调用浅述
查看>>
CodeCombat森林关卡Python代码
查看>>
第一个应用程序HelloWorld
查看>>
(二)Spring Boot 起步入门(翻译自Spring Boot官方教程文档)1.5.9.RELEASE
查看>>
Java并发编程73道面试题及答案
查看>>
企业级负载平衡简介(转)
查看>>
ICCV2017 论文浏览记录
查看>>
科技巨头的交通争夺战
查看>>
当中兴安卓手机遇上农行音频通用K宝 -- 卡在“正在通讯”,一直加载中
查看>>
Shell基础之-正则表达式
查看>>
JavaScript异步之Generator、async、await
查看>>
讲讲吸顶效果与react-sticky
查看>>