You are on page 1of 2

Laboratorio 02

Procesamiento de Imágenes
# importar imagen
import cv2 img =
cv2.imread('./image/imgproc01.jpg')
cv2.imshow('Importar Imagen', img)
cv2.waitKey()

# importar imagen como escala de grises import cv2 img =


cv2.imread('./image/imgproc02.jpg', cv2.IMREAD_GRAYSCALE)
cv2.imshow('Escala de Grises', img) cv2.waitKey()

# convertir formato .jpg a .png import cv2 img = cv2.imread('./image/imgproc03.jpg')


cv2.imwrite('./image/imgproc03.png', img, [cv2.IMWRITE_PNG_COMPRESSION])

# convertir espacio de color BGR a YUV import cv2 imgbgr =


cv2.imread('./image/imgproc05.jpg', cv2.IMREAD_COLOR) imgyuv =
cv2.cvtColor(imgbgr, cv2.COLOR_BGR2YUV) cv2.imshow('Imagen
YUV', imgyuv) cv2.waitKey()

# dividir espacios de color YUV import cv2 imgbgr =


cv2.imread('./image/imgproc05.jpg', cv2.IMREAD_COLOR) imgyuv =
cv2.cvtColor(imgbgr, cv2.COLOR_BGR2YUV) y,u,v =
cv2.split(imgyuv) cv2.imshow('Canal Y', y) cv2.imshow('Canal U', u)
cv2.imshow('Canal V', v) cv2.waitKey()

# dividir canales Y, U y V.
import cv2
imgbgr = cv2.imread('./image/imgproc06.jpg', cv2.IMREAD_COLOR)
imgyuv = cv2.cvtColor(imgbgr, cv2.COLOR_BGR2YUV) y,u,v =
cv2.split(imgyuv) cv2.imshow('Canal Y', imgyuv[:,:,0])
cv2.imshow('Canal U', imgyuv[:,:,1]) cv2.imshow('Canal V',
imgyuv[:,:,2]) cv2.waitKey()

1
# funsionar canales de imagen import cv2 imgbgr =
cv2.imread('./image/imgproc07.jpg', cv2.IMREAD_COLOR) g,b,r =
cv2.split(imgbgr)
rbrimg = cv2.merge((r,b,r)) grrimg =
cv2.merge((g,r,r)) bbrimg =
cv2.merge((b,b,r)) bgrimg =
cv2.merge((g,b,r)) cv2.imshow('Imagen
Original', imgbgr)
cv2.imshow('Fusionado RBR', rbrimg)
cv2.imshow('Fusionado BBR', bbrimg)
cv2.imshow('Fusionado BGR', bgrimg)
cv2.waitKey()

# trasladar imagen import cv2 import numpy as np img =


cv2.imread('./image/imgproc08.jpg') numfils, numcols =
img.shape[:2] matriz = np.float32([[1,0,20],[0,1,40]]) imgtras
= cv2.warpAffine(img, matriz, (numcols, numfils))
cv2.imshow('Imagen Trasladado', imgtras) cv2.waitKey()

# trasladar imagen import cv2 import numpy as np img =


cv2.imread('./image/imgproc09.jpg') numfils, numcols = img.shape[:2]
matriz = np.float32([[1,0,20],[0,1,40]]) imgtras = cv2.warpAffine(img,
matriz, (numcols, numfils+40)) matriz = np.float32([[1,0,-10],[0,1,-30]])
imgtras = cv2.warpAffine(imgtras, matriz, (numcols+0, numfils+20))
cv2.imshow('Imagen Trasladado', imgtras) cv2.waitKey()

# aplicar borde borroso


import cv2 import
numpy as np
img = cv2.imread('./image/imgproc10.jpg') numfils, numcols = img.shape[:2] matriz =
np.float32([[1,0,20],[0,1,40]]) imgtras = cv2.warpAffine(img, matriz, (numcols,
numfils),cv2.INTER_LINEAR, cv2.BORDER_WRAP, 1) cv2.imshow('Imagen Trasladado', imgtras)
cv2.waitKey()

# rotar imagen import


cv2
img = cv2.imread('./image/imgproc11.jpg') numfils,
numcols = img.shape[:2]
matrizrot = cv2.getRotationMatrix2D((numcols/2, numfils/2), 30, 0.7)
imgrot = cv2.warpAffine(img, matrizrot, (numcols, numfils))
cv2.imshow('Imagen Rotado', imgrot) cv2.waitKey()

# rotar y trasladar imagen import cv2 import numpy as np img =


cv2.imread('./image/imgproc12.jpg') numfils, numcols = img.shape[:2]
matriztra = np.float32([[1,0,int(0.5*numcols)],[0,1,int(0.5*numfils)]])
matrizrot = cv2.getRotationMatrix2D((numcols, numfils), 30, 1) imgtra
= cv2.warpAffine(img, matriztra, (2*numcols, 2*numfils)) imgrot =
cv2.warpAffine(imgtra, matrizrot, (numcols*2, numfils*2))
cv2.imshow('Rotacion', imgrot) cv2.waitKey()

You might also like