GraphicsMagic, create new image with another image centered in it - graphicsmagick

I have an image I1 of size AxB and want to create a bigger image I2 with a solid background of size CxD where C>A and D>B. I need to put I1 in I2 so that upper-left corner of I1 be at X,Y point of I2.
How can I do this with GraphicsMagic tools?

Like this:
gm convert -size 1200x1000 xc:steelblue -draw 'image over 10,200 600,800 "tux.png"' result.png
where C=1200, D=1000, x=10, y=200
Or, less flexible over the positioning:
gm convert -background steelblue "tux.png" -gravity center -extent 1200x1200 result.png

Related

Detect a specific line from a image using R

I am new to image processing. I want to detect a specific line in this image, which is the horizontal line in the middle of it. I am wondering how to approach it. This is the map, and it has already been simplified to the edge map.
Here's one method.
Let's start by reading in your image. We'll use the png library for this, so if you don't have it installed, you'll want to install.packages("png") first.
library(png)
img_link <- "https://i.stack.imgur.com/PygvJ.png"
img <- readPNG(readBin(img_link, "raw", 1e6))
The readPNG function extracts the image as an array. In this case, the array is 360 by 371 by 4:
dim(img)
#> [1] 360 371 4
This means the image is 360 by 371 pixels and contains four channels: red, blue, green and alpha (transparency).
We can plot this array at any time by calling:
plot(as.raster(img))
To find a horizontal red line, all we need to do is look at the row sums of the pixel values in the green channel, which will drop sharply at the target row.
sum_green <- apply(img[,,2], 1, sum)
We can plot this vector to ensure there is a row with a large drop-off in one of the rows, as we would expect.
plot(sum_green, type = "l")
To get this row, we just find the minimum row sum:
target_row <- which.min(sum_green)
target_row
#> [1] 183
So our red line is on the 183rd row. To prove this, let's make the 183rd row of our image black and draw it again:
img[target_row, , 1:3] <- 0
plot(as.raster(img))
This looks correct.

R - Getting Color Values [duplicate]

This question already has answers here:
extract RGB channels from a jpeg image in R
(3 answers)
Closed 6 years ago.
I have a jpeg image that I am attempting to extract the RGB values from in R.
Here is the image:
I am able to access the pixel values quite easily with the following code:
library(jpeg)
y <- readJPEG("MOLD_1.jpg")
head(y)
This returns:
[1] 0.9450980 0.9450980 0.9450980 0.9490196 0.9490196 0.9529412
I'm interested in knowing the color values associated with those pixels. I've tried to use alot of packages to figure this out including raster, pixmap, etc.
I'm struggling pretty bad - any help would be appreciated.
readJPEG returns a 3-D array that is height x width x channels. You can access individual color values using standard indexing. For example, y[,,1] will give you a height x width matrix of red intensities. You can convert these to color values using the rgb() function:
val <- rgb( y[,,1], y[,,2], y[,,3] )
myImg <- matrix( val, dim(y)[1], dim(y)[2] )

Vectorizing raster brick objects with r-raster so that I can count them

I have an image of columns of red and blue bordered circles like so:
Where the columns alternate red and blue (in this example the first column is red)
I have been able to create a raster brick and plot the image in RGB layers but I want to count these columns into a vector like this (from above example). Values 1(red) and 2(blue)
1,1,1,1,2,2,2,1,1,2,1,1,1 ...
Is it possible to clear out areas of the brick I don't need for counting and collapse the brick down into values I could then convert into the numbers or labels I want? Or is there a much simpler way that I'm unable to locate? Also long term I want to be able to point the program at several images without opening them myself.
Edit: To clear somethings up, I want to count the circles top to bottom, left to right. For example once the first col is counted, I want to start over at the top of the next column on the right. Also I'm not sure if I'm headed in the right direction but I was able to remove all background cells from the image. leaving me with a plot of only values where the circles are.
Edit 2:
The current code I have for the image above.
color.image <- brick("image")
color.image = dropLayer(color.image,4) #gets rid of a channel
plot(color.image)
e <- extent(-10, 240, 45, 84.8) #xmin,xmax, ymin,ymax
ccolor.image <- crop(color.image, e)
plot(ccolor.image)
#thresholding to simplify what I was dealing with
mini=ccolor.image[ccolor.image > 97] = NA
mini=ccolor.image[ccolor.image < 15] = NA
mini=ccolor.image[ccolor.image > 20] = 80
plot(ccolor.image)
mcolor = as.matrix(ccolor.image)
colSums(ccolor.image)
rowSums(ccolor.image)
Edit 3:
I figured it out! Or at least found a round about way to do it, will post code later once I clean it up some. I still however would like input on creating a vector based on the matrix of values I have for my simplified raster brick matrix. Code coming soon!
The fastest way to count values in a raster is freq(x, merge=T). This will give you the value in one column and the frequency in as many columns as you have rows. In this way we the need to poll a value of interest and sum all the other columns (the counts). Hope that helps!
freq_vals <- freq ( rasterbrick , merge = T )
sum( freq_vals [ which ( freq_vals$value == 1 ) , 2 : ncol ( freq_vals ) ] )

How would i find what column and row the mouse is in?

I need to find what column and row the mouse location is in. To simplify this question, lets only find the column. I will write in pseudocode.
I have a map (a grid of rows and columns, made up by square cells) with a pixel width. I have a cell size which makes up each columns pixel width.
eg map.width / cell size = map.NumberOfColumns.
From this we can get what column the mouse is on.
Eg if ( mouse.X > cellSize ) {col is definitely > 1} (i have not used zero indexing in this example).
So if anyone here loves maths, i would very much appreciate some help. Thanks.
Assuming square cells, 1-based row/col indexing, and truncating integer division:
col = mouse.X / cellSize + 1;
row = mouse.Y / cellSize + 1;

How to find the vertexes of a flat wall given it's center pos, it's normal, it's width and it's length?

I'm defining a flat wall as a center pos (cx,cy,cz), a normal (nx,ny,nz), a vector pointing to the up-direction of the wall (ux,uy,uz) it's width and length (w,l). How do I find the position of it's 4 vertexes?
I'll assume that by length, you mean height. First, make sure that your up and normal vectors are normalized. You can multiply the up vector by the length, and add and subtract the result from the center to get the temporary results A and B, respectively.
Then, cross product the up vector with the normal vector to get the right vector (or left, depending on what order you do the cross product). Then multiply the right vector by the width, and add and subtract this from the center to get two more temporary results, C and D, respectively.
Finally, the four corners of the quad can be obtained by adding each of C and D to each of A and B.

Resources