测量jpeg中的空白 [英] Measuring whitespace in a jpeg

查看:109
本文介绍了测量jpeg中的空白的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想测量一个白色/黄色的jpeg的数量(在可以调整的公差范围内)。

I want to measure the amount of a jpeg that is white/yellow (within a tolerance that can be adjusted).

我正在尝试开发质量控制测量杏仁缺陷的工具。缺陷是棕色杏仁皮肤划痕(见下图)。由于这些缺陷是白色/黄色,我想要一种简单地将图像加载到R中的方法,并让它测量白色图像的数量。然后我可以通过实验确定可接受的水平。所有图片的大小都相同。

I am trying to develop a quality control tool that measures defects in almonds. The defects are scratches in the skin of brown almonds (see image below). Since these defects are white/yellow I would like a way to simply load the image into R and and have it measure the amount of the image that is white. I can then experimentally determine an acceptable level. All images would be the same size.

推荐答案

Carl的帖子是答案的99%,这里有一点点来衡量一下图像的数量白色/近白色:

Carl's post is 99% of the answer, here's a tiny bit more to get a measurement of the amount of the image that is white/near-white:

# Required package
library(jpeg)

# Load and plot data
jpg <- "C:\\my_image.jpg"
my_jpg <- readJPEG(jpg)

# or for stand-alone reproducibility: 
# my_jpg <- readJPEG(system.file("img", "Rlogo.jpg", package="jpeg"))

# have a look at the original image
plot(0:1,0:1,type="n",ann=FALSE,axes=FALSE)
rasterImage(my_jpg,0,0,1,1)
# prints the jpg, just to make sure it's gone in ok

# Following Carl's example, subset each channel to get
# the pixels with white values (ie. close to 1) and make
# non-white pixels black for convienence. As Carl says,
# you'll need to adjust the values from 0.99 for your
# use case 
white_red_channel <- ifelse(my_jpg[,,1] > 0.99, 1,0)
white_green_channel <- ifelse(my_jpg[,,2] > 0.99, 1,0)
white_blue_channel <- ifelse(my_jpg[,,3] > 0.99, 1,0)
# combine channels into array
white <- simplify2array(list(white_red_channel, 
                             white_green_channel, 
                             white_blue_channel))

# plot white/near-white pixels only
plot(0:1,0:1,type="n",ann=FALSE,axes=FALSE)
rasterImage(white, 0, 0, 1, 1)
# looks pretty good, whiter areas on original are highlighted here:

# find proportion of image that is not black
whites <- white_red_channel + white_green_channel + white_blue_channel # sum channels
not_black <- sum(whites > 0) # count pixels that are not black
total_pixels <- ncol(whites) * nrow(whites) # find total number of pixels
not_black / total_pixels # proportion of non-black pixels
[1] 0.01390833

这篇关于测量jpeg中的空白的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆