How do I capture the screen using Ruby? - ruby

I need to capture the screen using Ruby, and then get an array of RGB pixel values for each pixel on the screen.
I tried the Windows API, and can bitblt the screen and retrieve the handle of the bitmap, but I have no idea how to access the raw RGB values within this handle's data.
This is what I have at the moment, and it's fast enough, but I need to get the RGB values from the hbitmap into an array that I can work with.
Anything as fast as Bitblt but easier would be appreciated too.
def getscreen()
width = Win32API.new("User32.dll","GetSystemMetrics",["L"],"L").call(0)
height = Win32API.new("User32.dll","GetSystemMetrics",["L"],"L").call(1)
#Get desktop DC, create a compatible dc, create a comaptible bitmap and select into compatible dc.
hddc = Win32API.new("User32.dll","GetDC",["L"],"L").call(Win32API.new("User32.dll","GetDesktopWindow",[],"L").call)
hcdc = Win32API.new("Gdi32.dll","CreateCompatibleDC",["L"],"L").call(hddc)
hbitmap = Win32API.new("Gdi32.dll","CreateCompatibleBitmap",["L","L","L"],"L").call(hddc,width,height)
Win32API.new("Gdi32.dll","SelectObject",["L","L"],"L").call(hcdc,hbitmap)
Win32API.new("Gdi32.dll","BitBlt",["L","L","L","L","L","L","L","L","P"],"L").call(hcdc,0,0,width,height,hddc,0,0,"SRCCOPY|CAPTUREBLT")
#save hbitmap to stream of byte as you mentioned
puts hbitmap
#
Win32API.new("User32.dll","ReleaseDC",["L","L"],"L").call(Win32API.new("User32.dll","GetDesktopWindow",[],"L").call,hddc)
Win32API.new("Gdi32.dll","DeleteDC",["L"],"L").call(hcdc)
Win32API.new("Gdi32.dll","DeleteObject",["L"],"L").call(hbitmap)
#Print screen width and height
puts "Screen width: #{width}"
puts "Screen height: #{height}"
end

I figured out how to do this so I though I would post the solution for others who may need help.
The GetDIBits Win32API function can be used to access the RGB array stored in the bitmap captured using the above code.
http://msdn.microsoft.com/en-us/library/dd144879%28v=vs.85%29.aspx

Related

Manipulate pixels using Minimagick

Is there a way to manipulate pixels and create or modify the manipulated image to reflect the changes made to the pixels ?
Minimagick only provides a get_pixels method.
Should I convert the array to a string and use the import_pixels method ? but then, how can I reconvert the pixels to a blob ?
Correct, you should use the import_pixels method. Here's a full example:
# get pixels
img = MiniMagick::Image.open("image.jpg")
pixels = img.get_pixels
# transform pixels
reverse = pixels.map(&:reverse)
# save pixels
blob = reverse.flatten.pack("C*")
img = MiniMagick::Image.import_pixels(blob, img.width, img.height, 8, "rgb", "jpg")
img.write("reverse.jpg")

Writing a greyscale video using Videowriter/avifile

I am writing a function that generates a movie mimicking a particle in a fluid. The movie is coloured and I would like to generate a grayscaled movie for the start. Right now I am using avifile instead of videowriter. Any help on changing this code to get grayscale movie? Thanks in advance.
close all;
clear variables;
colormap('gray');
vidObj=avifile('movie.avi');
for i=1:N
[nx,ny]=coordinates(Lx,Ly,Nx,Ny,[x(i),-y(i)]);
[xf,yf]=ndgrid(nx,ny);
zf=zeros(size(xf))+z(i);
% generate a frame here
[E,H]=nfmie(an,bn,xf,yf,zf,rad,ns,nm,lambda,tf_flag,cc_flag);
Ecc=sqrt(real(E(:,:,1)).^2+real(E(:,:,2)).^2+real(E(:,:,3)).^2+imag(E(:,:,1)).^2+imag(E(:,:,2)).^2+imag(E(:,:,3)).^2);
clf
imagesc(nx/rad,ny/rad,Ecc);
writetif(Ecc,i);
if i==1
cl=caxis;
else
caxis(cl)
end
axis image;
axis off;
frame=getframe(gca);
cdata_size = size(frame.cdata);
data = uint8(zeros(ceil(cdata_size(1)/4)*4,ceil(cdata_size(2)/4)*4,3));
data(1:cdata_size(1),1:cdata_size(2),1:cdata_size(3)) = [frame.cdata];
frame.cdata = data;
vidObj = addframe(vidObj,frame);
end
vidObj = close(vidObj);
For your frame data, use rgb2gray to convert a colour frame into its grayscale counterpart. As such, change this line:
data(1:cdata_size(1),1:cdata_size(2),1:cdata_size(3)) = [frame.cdata];
To these two lines:
frameGray = rgb2gray(frame.cdata);
data(1:cdata_size(1),1:cdata_size(2),1:cdata_size(3)) = ...
cat(3,frameGray,frameGray,frameGray);
The first line of the new code will convert your colour frame into a single channel grayscale image. In colour, grayscale images have all of the same values for all of the channels, which is why for the second line, cat(3,frameGray,frameGray,frameGray); is being called. This stacks three copies of the grayscale image on top of each other as a 3D matrix and you can then write this frame to your file.
You need to do this stacking because when writing a frame to file using VideoWriter, the frame must be colour (a.k.a. a 3D matrix). As such, the only workaround you have if you want to write a grayscale frame to the file is to replicate the grayscale image into each of the red, green and blue channels to create its colour equivalent.
BTW, cdata_size(3) will always be 3, as getframe's cdata structure always returns a 3D matrix.
Good luck!

qtruby draw picture point by point

Hi I'm trying to write BMP reader writer in ruby and now i'm stuck on write it on screen.
I have picture stored in pixels array and on every pixel is stored rgb color.
But nothing happens in in window? What I'm doing wrong? Or is there any qt object to which i can stored pixel data and simply paint it?
def initialize
super
setWindowTitle "Transparent rectangles"
resize 590, 90
move 300, 300
show
end
def paintEvent event
painter = Qt::Painter.new self
bmp = BMP::Reader.new("picture.bmp")
drawPicture(painter,bmp.getPixels())
painter.end
end
def drawPicture(painter, pixels)
painter.setPen Qt::NoPen
0.upto(pixels.length-1) do |i|
0.upto(pixels[0].length-1) do |j|
painter.setBrush Qt::Brush.new Qt::Color.new pixels[i][j][2], pixels[i][j][1], pixels[i][j][0], 255
painter.drawPoint(i,j)
end
end
end
QPainter.drawPoint uses the current pen, not the brush. Call painter.setPen before each point.
But you would be much better off storing the pixels in a QImage. Qt already has support for reading BMP files so there's no need to implement that yourself unless you have a good reason to.

How to get Intensity Pointer of the gray Image in opencv

I have a binary file of 16 bit Intensity of the image. I have read this data in short array. And create 16 bit gray Image using the following code.
IplImage *img=cvCreateImage( cvSize( Image_width, Image_height ), IPL_DEPTH_16S, 1 );
cvSetData(img,Data, sizeof(short )*Image_width);
Where Data is short array.
Then I set ROI for this Image using this function
cvSetImageROI(img, cvRect(crop_start.x, crop_start.y, crop_width, Image_height));
and ROI is being set successfully.
Now after setting the ROI I want to access the Intensity of the Image means I want pointer of the intensity of the cropped Image. I have tried this code to access the intensity
short *crop_Imagedata=(short *)img->imageData;
But This pointer is not giving the right Intensity as I have checked that by debugging the code.
Can any body please tell me how can I get pointer of the Image Intensity.
Thanks in Advance
Hello I have tryed the following to find what maybe you wannt to do:
IplImage *img=cvCreateImage( cvSize( 15, 15 ), IPL_DEPTH_16S, 1 );
cvSet(img,cvScalar(15));//sets all values to 15
cvSetImageROI(img, cvRect(4, 0, 10, 15));
short *crop_Imagedata=(short *)img->imageData;
((*crop_Imagedata) == 15) // true
The value that you will get is not in the roi! imageData of the IplImage structure is just a simple pointer and not a function !!! the rois of opencv is something that isn t that well documented and easy to use in my opinion. Maybe all algorithms of opencv use the rois somehow. I use them to but there is no such automatic function with the standard iplimage structure to simple use them.
If you wannt more magic try to use the new cv::Mat object...
but if you still wann t to use rois then you will have to allways to use
CvRect roi = cvGetImageROI(img);
method to check all the time the position. After that you will have to add the roi offset:
((short*)(img->imageData + (roi.y+ypos)*img-widthStep))[roi.x+xpos]
remember the standard Opencv is a C library not a C++ ! btw when mixing cv::Mat rois can be a bit annoying to. To copy an iplimage to a cv::Mat without roi I have to do the following:
CvRect roitmp = cvGetImageROI(ilimage);
cvResetImageROI(ilimage);
cv::Mat tmp = cv::Mat(ilimage).clone();
cvSetImageROI(ilimage,roitmp);
maybe here someone knows the right way of working with rois...

CreatePatternBrush and screen color depth

I am creating a brush using CreatePatternBrush with a bitmap created with CreateBitmap.
The bitmap is 1 pixel wide and 24 pixels tall, I have the RGB value for each pixel, so I create an array of rgbquads and pass that to CreateBitmap.
This works fine when the screen color depth is 32bpp, since the bitmap I create is also 32bpp.
When the screen color depth is not 32bpp, this fails, and I understand why it does, since I should be creating a compatible bitmap instead.
It seems I should use CreateCompatibleBitmap instead, but how do I put the pixel data I have into that bitmap?
I have also read about CreateDIBPatternBrushPt, CreateDIBitmap, CreateDIBSection, etc.
I don´t understand what is a DIBSection, and find the subject generally confusing.
I do understand that I need a bitmap with the same color depth as the screen, but how do I create it having only the 32bpp pixel data?
You could create a DIB because you can use a Device Independent Bitmap independently of the screen color depth. See CreateDIBSection().
How can you create it having only the 32bpp pixel data? A DIB can be created with 32bpp data. As you can read in the documentation:
The CreateDIBSection function creates
a DIB that applications can write to
directly. The function gives you a
pointer to the location of the bitmap
bit values.
If hSection is NULL, the system
allocates memory for the DIB. If the
function succeeds, the return value is
a handle to the newly created DIB, and
*ppvBits points to the bitmap bit values.
Try something like this:
VOID *ppvBits = NULL;
BITMAPINFO BitmapInfo;
memset(&BitmapInfo, 0, sizeof(BITMAPINFOHEADER));
BitmapInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
BitmapInfo.bmiHeader.biWidth = 1;
BitmapInfo.bmiHeader.biHeight = 24;
BitmapInfo.bmiHeader.biPlanes = 1;
BitmapInfo.bmiHeader.biBitCount = 32;
BitmapInfo.bmiHeader.biCompression = BI_RGB;
HBITMAP hBitmap = CreateDIBSection(hDC, &BitmapInfo, DIB_RGB_COLORS, &ppvBits, NULL, 0);
In our case *ppvBits points to 1 * 24 * (32 / 8) allocated bytes.
It is important to know that if biHeight is positive, the bitmap is a bottom-up DIB and its origin is the lower-left corner. See BITMAPINFOHEADER Structure for more info.
I solved it by using CreateCompatibleBitmap and SetPixel. Not the best option I guess, but it works.

Resources