0

I have an image of columns of red and blue bordered circles like so:

graph image

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!

989
  • 12,579
  • 5
  • 31
  • 53

1 Answers1

0

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 ) ] )
Badger
  • 1,043
  • 10
  • 25
  • I am unable to perform this on the rasterbrick object, it's only operating on the raster stack. Even then I don't think the output is going to allow me to count circles in the image from top to bottom, moving to the top of the next column if there is no circle. So counting top to bottom, left to right. – Antiparticle Oct 07 '15 at 21:43
  • I'm curious what your data looks like, you aren't counting circles per se, moreover you are counting a numeric value. It sounds like you are looking for a matrix of the information. `as.matrix()` may serve your purpose. and then use `colSums()`. – Badger Oct 07 '15 at 21:53
  • I can see something getting closer here. Thank you for the help by the way! Now I am getting three columns with values in them but the columns are the different channels of the raster, I only want to deal with one (this does not need any attention). It seems each column is the complete channel. How is the image represented in just one column? Is the image turned into a long vector and I need to find the length of the height of the image to put it together? – Antiparticle Oct 07 '15 at 22:10