前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Python OpenCV 图片反色、调

Python OpenCV 图片反色、调

作者头像
py3study
发布2020-01-07 11:52:58
1.5K0
发布2020-01-07 11:52:58
举报
文章被收录于专栏:python3

OpenCV 版本:3.0.0 Python版本:2.7.10

实现图像反色:

实现原理:

读取每个像素值P,再将255-P写入新的图片中;

对于灰度图,只有一个通道,所以 img2[i,j] = (255-image[i,j]) ;

对于彩色图片,则要RGB值分别做处理,255-image[i,j][0],255-image[i,j][1],255-image[i,j][2];

灰度图:

代码语言:javascript
复制
def inverse_color(image):

    height,width = image.shape
    img2 = image.copy()

    for i in range(height):
        for j in range(width):
            img2[i,j] = (255-image[i,j]) 
    return img2

彩色图:

代码语言:javascript
复制
def inverse_color(image):

    height,width,temp = image.shape
    img2 = image.copy()

    for i in range(height):
        for j in range(width):
            img2[i,j] = (255-image[i,j][0],255-image[i,j][1],255-image[i,j][2]) 
    return img2

调整图片大小:

有4种参数:

CV_INTER_NN - 最近邻插值,

CV_INTER_LINEAR - 双线性插值 (缺省使用)

CV_INTER_AREA - 使用象素关系重采样

CV_INTER_CUBIC - 立方插值.

代码语言:javascript
复制
def resize(img,width,height):
    res=cv2.resize(img,(width,height),interpolation=cv2.INTER_CUBIC)
    return res

遍历目录下所有图片并将图片进行反色处理、调整大小, 最后按照每张图片一行存储在txt中:

代码语言:javascript
复制
__author__ = 'geyalu'

import cv2
import os
""" Trans image to pixel data and saved in a txt  """


def list_dir(rootDir):
    """list all files in a direction and return img_path """
    img_path=[]
    for lists in os.listdir(rootDir):
        path = os.path.join(rootDir, lists)
        print path
        img_path.append(path)
        if os.path.isdir(path):
            list_dir(path)
    return img_path


def load_img(path):
    img = cv2.imread(path,0)
    #cv2.imshow('Image', img)
    #cv2.waitKey (0)
    #cv2.destroyAllWindows()
    return img


def img_to_list(img):
    """trans img to pixel data"""
    temp_data=[]
    height,width = img.shape
    for i in range(height):
        for j in range(width):
            temp=img[i,j]
            temp_data.append(str(temp))
    data_w=','.join(temp_data)
    return data_w


def inverse_color(image):

    height,width = image.shape
    img2 = image.copy()

    for i in range(height):
        for j in range(width):
            img2[i,j] = (255-image[i,j]) # For GRAY_SCALE image ;
                                         # for R.G.B image: img2[i,j] = (255-image[i,j][0],255-image[i,j][1],255-image[i,j][2])
    return img2


def resize(img,width,height):
    res=cv2.resize(img,(width,height),interpolation=cv2.INTER_CUBIC)
    return res



def main_resize_inverse():

    rootdir=r"F:\hand_number2"    #direction you want to traverse
    img_path=list_dir(rootdir)

    fp=open('hand_numbers2.txt','a') #results you want to save

    for i in img_path:
        img=load_img(i)
        img_res=resize(img,28,28)
        img_res_inverse=inverse_color(img_res)


        cv2.imshow('Image', img_res_inverse)
        cv2.waitKey (0)
        cv2.destroyAllWindows()

        img_data=img_to_list(img_res_inverse)

        fp.writelines(img_data)
        fp.write('\n')

    fp.close()


def main_only_Trans_to_txt():

    rootdir=r"F:\number9"    #direction you want to traverse
    img_path=list_dir(rootdir)

    fp=open('hand_numbers.txt','a') #results you want to save

    for i in img_path:
        img=load_img(i)
        img_data=img_to_list(img)

        fp.writelines(img_data)
        fp.write('\n')

    fp.close()


if __name__ == '__main__':
    """Choose main function
        main_resize_inverse()
        main_only_Trans_to_txt()
    """

    main_only_Trans_to_txt()
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019/09/10 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 实现图像反色:
  • 遍历目录下所有图片并将图片进行反色处理、调整大小, 最后按照每张图片一行存储在txt中:
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档