How to use readClipboard to read a bitmap when using R? - r

I don't managed to read a bitmap which is in the Windows clipbard.
I first check the cliboard :
getClipboardFormats()
which give
[1] "bitmap" "DIB" "shell"
Then I tried :
image = readClipboard(format=2, raw=FALSE)`
and
image = readClipboard(format=2, raw=TRUE)
but I always obtain a NULL value for image !
Can someone explain to me how to do it?

Related

How to convert QImage to QVideoFrame so I would set it as frame for VideoSink Qt6.2

I'm working on a QML Qt6.2 app that receives images that I would like to display to the user, I am using a VideoOutput and a QVideoSink to change the frames, however I have to convert my images to QVideoFrames before I can set them as frames for the sink, I need some help on how to do that.
Since they changed the VideoOutput handling in Qt6 I cannot seem to find and relevant help surfing the web.
Such method does the job for me
QVideoFrame frame(QVideoFrameFormat(img.size(), QVideoFrameFormat::pixelFormatFromImageFormat(img.format()));
frame.map(QVideoFrame::ReadWrite);
memcpy(frame.bits(0), img.bits(), img.sizeInBytes());
frame.unmap();
m_videoSink->setVideoFrame(frame);
however generating the videoframe format from an image format returns an invalid frame, explicitly providing a format does show the image but it's messed up due to incompatible formats, so now I want to find a videoframe format that would show my RGB888 formatted image as is,
memcpy does the job as an answer to the question, and if the image format is compatible with the supported frame formats the code block above would do .
QImage img( ... Any valid QImage with any format ... );
img = img.convertToFormat( QImage::Format_ARGB32 );
QVideoFrameFormat fmt( img.size(), QVideoFrameFormat::Format_YUYV );
QVideoFrame vf( fmt );
vf.map( QVideoFrame::WriteOnly );
libyuv::ARGBToYUY2( (const uint8_t*) img.bits(), img.bytesPerLine(), vf.bits( 0 ),
vf.bytesPerLine( 0 ), img.width(), img.height() );
vf.unmap();

In Godot the path returned by get_simple_path() seems offset by something

Just learning Godot, so maybe missing something obvious
I am trying to have the player navigate towards a point clicked on the map.
The path is calculated with some sort of offset I can't figure out.
Any pointers appreciated!
There is a very minimal replication of the problem here
https://github.com/kender99/Godot_path_finding_problem_demo
On the image the white dot is the mouse click and the red is the path generated
The likely offending code is:
extends Node2D
var path : = PoolVector2Array()
func _unhandled_input(event):
if event is InputEventMouseButton:
if event.button_index == BUTTON_LEFT and event.pressed:
path = $Navigation2D.get_simple_path($Player.position, event.position)
$Player.path = path
$Line2D.points = path
print(path.size(), ' Path:',path, ' Player:', $Player.position, ' Target:', event.position)
update() # so line and circles get drawn
func _draw():
for p in path:
draw_circle(p, 5, Color(200, 200, 200))
event.position is the local position regards the current respondent, you should convert the event position to the local one of Navigation2D instead, like this:
path = $Navigation2D.get_simple_path($Player.position, $Navigation2D.to_local(event.position))
or use get_global_mouse_position() method like this:
path = $Navigation2D.get_simple_path($Player.position, $Navigation2D.to_local(get_global_mouse_position()))

DM Script to import a 2D image in text (CSV) format

Using the built-in "Import Data..." functionality we can import a properly formatted text file (like CSV and/or tab-delimited) as an image. It is rather straight forward to write a script to do so. However, my scripting approach is not efficient - which requires me to loop through each raw (use the "StreamReadTextLine" function) so it takes a while to get a 512x512 image imported.
Is there a better way or an "undocumented" script function that I can tap in?
DigitalMicrograph offers an import functionality via the File/Import Data... menu entry, which will give you this dialog:
The functionality evoked by this dialog can also be accessed by script commands, with the command
BasicImage ImageImportTextData( String img_name, ScriptObject stream, Number data_type_enum, ScriptObject img_size, Boolean lines_are_rows, Boolean size_by_counting )
As with the dialog, one has to pre-specify a few things.
The data type of the image.
This is a number. You can find out which number belongs to which image data type by, f.e., creating an image outputting its data type:
image img := Realimage( "", 4, 100 )
Result("\n" + img.ImageGetDataType() )
The file stream object
This object describes where the data is stored. The F1 help-documention explains how one creates a file-stream from an existing file, but essentially you need to specify a path to the file, then open the file for reading (which gives you a handle), and then using the fileHandle to create the stream object.
string path = "C:\\test.txt"
number fRef = OpenFileForReading( path )
object fStream = NewStreamFromFileReference( fRef, 1 )
The image size object
This is a specific script object you need to allocate. It wraps image size information. In case of auto-detecting the size from the text, you don't need to specify the actual size, but you still need the object.
object imgSizeObj = Alloc("ImageData_ImageDataSize")
imgSizeObj.SetNumDimensions(2) // Not needed for counting!
imgSizeObj.SetDimensionSize(0,10) // Not used for counting
imgSizeObj.SetDimensionSize(1,10) // Not used for counting
Boolean checks
Like with the checkboxes in the UI, you spefic two conditions:
Lines are Rows
Get Size By Counting
Note, that the "counting" flag is only used if "Lines are Rows" is also true. Same as with the dialog.
The following script improrts a text file with couting:
image ImportTextByCounting( string path, number DataType )
{
number fRef = OpenFileForReading( path )
object fStream = NewStreamFromFileReference( fRef, 1 )
number bLinesAreRows = 1
number bSizeByCount = 1
bSizeByCount *= bLinesAreRows // Only valid together!
object imgSizeObj = Alloc("ImageData_ImageDataSize")
image img := ImageImportTextData( "Imag Name ", fStream, DataType, imgSizeObj, bLinesAreRows, bSizeByCount )
return img
}
string path = "C:\\test.txt"
number kREAL4_DATA = 2
image img := ImportTextByCounting( path, kREAL4_DATA )
img.ShowImage()

Saving a cropped image in R

I am using jpeg package to read an image into R. This creates an object of class nativeRaster. I take a portion of this image using [ operator. The resulting object is a matrix of integers. Attempting to save this object returns the error image must be a matrix or array of raw or real numbers. What should I do to be able to save this new image?
Below is a snippet to reproduce the error
imageFile = 'address of the jpg file'
outputFile = 'new file to write into'
image = jpeg::readJPEG(imageFile, native=TRUE)
output = image[1:10,1:10]
writeJPEG(image = output, target = outputFile)
I think the function writeJPEG takes image of type nativeRaster. I am not entirely sure about this but converting class of output to nativeRaster works for me.
class(output) <- "nativeRaster"
writeJPEG(image = output, target = outputFile)
Even though I was unable to find a solution, I am working around this by using solely imagemagick to crop the image.
system(paste('identify',
imageFile), intern = TRUE) %>%
regmatches(.,regexpr("(?<=[ ])[0-9]*?x[0-9]*?(?=[ ])",.,perl=T)) %>%
strsplit('x') %>% .[[1]] %>% as.double
will return the dimensions of the image and
system(paste0('convert "',imageFile,
'" -crop ',
sizeX,
'x',
sizeY,
'+',
beginningX,
'+',
beginningY,
' "',
outputFile,'"'))
will crop the image and save it to outputFile
Here sizeX and sizeY are desired dimensions of the cropped image and beginningX and beginningY designate the top left corner of the crop site on the image.

Reading DICOM 1.2.840.10008.1.2.4.70

I have a problem when reading DICOM file. This is the format 1.2.840.10008.1.2.4.70(Process 14 with a first-order prediction (Selection Value 1). I write my own software.
Here is result of my work.
I also give you a .dcm file.
What can be wrong with it? Only RadiAnt Dicom Viewer open it corectly ( i didn't find any working software with source code ).
Have someone any tutorial about it? Any working code?
I'll be very grateful!
Thanks for help.
I show you how I it do:
//I have:
numCOL= imageWidth;
numROW= imageHeight;
dwCurrentBufferLength;//-> where I in file
//and other stuff...
//First i decode first row:
//[0][0]
DecodeFirstRow(curRowBuf,dwCurrentBufferLength);
//I calculate difrences
HuffDecode ( table , &val, dwCurrentBufferLength);
//and extend
HuffExtend(extend, val);
curRowBuf[0][curComp]=extend+(1<<(Pr-Pt-1));
//[1-n][0]
//... huff stuff
curRowBuf[col][curComp]=extend+curRowBuf[col-1][curComp];
//Then i put row to the vector:
for (col = 0; col < numCol; col++)
{
v=RowBuf[col][0]<<point_transform_parameter;
m_vOutputBuf.push_back(v);
}
//Rest of columns
//[0][m]
curRowBuf[0][curComp]=extend+prevRowBuf[0][curComp];
predictor = left =curRowBuf[leftcol][curComp];
//[1-n][m]
curRowBuf[col][curComp]=extend+predictor;
//and also put it to vector ^^
Where i must sub this 1000??
Most likely you have not accounted for the Rescale Intercept, tag (0028,1052) when calculating your HU values. According to the DICOM file, the intercept is -1000.
To get the appropriate HU values of your image, use this formula:
HU = rescale_slope * pixel_value + rescale_intercept
where Rescale Intercept is thus obtained from tag (0028,1052) and Rescale Slope from tag (0028,1053).
Here is what I see using gdcmviewer:
$ gdcmviewer 3DSlice1.dcm
GDCM is open source you could study it.

Resources