grayscale.py
import pyopencl as cl
import sys
import Image
import numpy
def RoundUp(groupSize, globalSize):
r = globalSize % groupSize;
if r == 0:
return globalSize;
else:
return globalSize + groupSize - r;
def main():
imageObjects = [ 0, 0 ]
if len(sys.argv) != 3:
print "USAGE: " + sys.argv[0] + " <inputImageFile> <outputImageFile>"
return 1
# create context and command queue
ctx = cl.create_some_context()
queue = cl.CommandQueue(ctx)
# load image
im = Image.open(sys.argv[1])
if im.mode != "RGBA":
im = im.convert("RGBA")
imgSize = im.size
buffer = im.tostring()
# Create ouput image object
clImageFormat = cl.ImageFormat(cl.channel_order.RGBA,
cl.channel_type.UNSIGNED_INT8)
imageObjects[0] = cl.Image(ctx,
cl.mem_flags.READ_ONLY | cl.mem_flags.COPY_HOST_PTR,
clImageFormat,
imgSize,
None,
buffer)
imageObjects[1] = cl.Image(ctx,
cl.mem_flags.WRITE_ONLY,
clImageFormat,
imgSize)
# load the kernel source code
kernelFile = open("grayscale.cl", "r")
kernelSrc = kernelFile.read()
# Create OpenCL program
program = cl.Program(ctx, kernelSrc).build()
# Call the kernel directly
localWorkSize = ( 16, 16 )
globalWorkSize = ( RoundUp(localWorkSize[0], imgSize[0]),
RoundUp(localWorkSize[1], imgSize[1]) )
program.rgbaToGrayscale(queue,
globalWorkSize,
localWorkSize,
imageObjects[0],
imageObjects[1])
# Read the output buffer back to the Host
buffer = numpy.zeros(imgSize[0] * imgSize[1] * 4, numpy.uint8)
origin = ( 0, 0, 0 )
region = ( imgSize[0], imgSize[1], 1 )
cl.enqueue_read_image(queue, imageObjects[1],
origin, region, buffer).wait()
print "Executed program succesfully."
# Save the image to disk
gsim = Image.fromstring("RGBA", imgSize, buffer.tostring())
gsim.save(sys.argv[2])
main()
grayscale.cl
const sampler_t sampler = CLK_NORMALIZED_COORDS_FALSE |
CLK_ADDRESS_CLAMP_TO_EDGE |
CLK_FILTER_NEAREST;
__kernel void rgbaToGrayscale(__read_only image2d_t srcImg,
__write_only image2d_t dstImg)
{
// Converts RGBA image to gray scale intensity using the following formula:
// I = 0.2126 * R + 0.7152 * G + 0.0722 * B
int2 coord = (int2) (get_global_id(0), get_global_id(1));
int width = get_image_width(srcImg);
int height = get_image_height(srcImg);
if (coord.x < width && coord.y < height)
{
uint4 color = read_imageui(srcImg, sampler, coord);
float luminance = 0.2126f * color.x + 0.7152f * color.y + 0.0722f * color.z;
color.x = color.y = color.z = (uint)luminance;
// Write the output value to image
write_imageui(dstImg, coord, color);
}
}
- 大小: 1.3 MB
- 大小: 5.7 MB
分享到:
相关推荐
Reversible Data Hiding in Color Image with Grayscale Invariance 论文代码matlab
convert rgb to grayscale
Fading from color to grayscale and vice-versa淡色效果(7KB)
Drawing an image in grayscale画位图到一个灰色刻度中(5KB)
-color-matrix matrix apply color correction to the image -contrast enhance or reduce the image contrast -contrast-stretch geometry improve contrast by `stretching' the intensity range -convolve ...
this is code for basic processing in image processig suc as grayscaling, negatif picture, and others.
You can just combine the intensities from all the color channels for the pixels into one long vector, as if you were working with a grayscale image with 3x the number of pixels as the original image.
本文提出一个新的图像对比度增强方法,这种基于多组件的直方图均衡化图像增强方法与古典直方图均衡化方法相比多个灰度阈值,以允许连接成分分析两个局部和全局对比度增强和最低畸变图像出现。
» Create and edit icons in standard and custom sizes, with color depth up to 32-bit True Color » Make stylish icons from your images » Paint images with gradients and fills » Create multi-layer ...
procedure GrayScale(const Image:TImage) var BytesPerScan: Integer iWidth, iHeight: Integer pScanLine: pByteArray R,G,B: Byte begin try BytesPerScan := Abs(Integer(Image.Picture.Bitmap....
灰度图Gray Scale Image 定义
语言:English (United States) 将任何灰度图像转换为彩色 Ampare灰度图像到彩色是一个简单易用的Chrome扩展程序,可将那些复杂的功能隐藏在后面。 只需单击一下按钮,它就可以使用深度学习神经元网络人工智能将任何...
提取糖尿病视网膜病变眼底... The color image to grayscale on grayscale morphological processing to the background, threshold segmentation, ultimately on the original hard exudates area marked in green.)
Monochrome, grayscale, color; With vertical and horizontal orientation of bytes; 8, 16, 24, 32-bit data; 1...24 bits per pixel; RLE compression; With different display controllers, not limited by one ...
Grayscale image Compression and Encryption Based on Format Conversion
It features multi-threaded batch image resizing, conversion, cropping, flipping/rotating, watermarks, decolorizing (grayscale, negative, sepia), and optimizing. The BMP, GIF, TIFF, JPEG, PNG, and EMF...
ImageViewer ActiveX控件 是一个ActiveX 控件,您可以利用它在您开发的程序中集成图片浏览功能,支持大部分常见的图片格式,可用于Microsoft Visual Studio,Microsoft Office 和 Borland Delphi。 <br> ...
% Variation Norm and Applications to Color Image Processing", UCLA CAM % Report 07-25. % % Example: % f = double(imread('barbara-color.png'))/255; % f = f + randn(size(f))*16/255; % u = tvdenoise(f,12...
grayscale edge detection code