gpt4 book ai didi

r - 测量 jpeg 中的空白

转载 作者:行者123 更新时间:2023-12-02 15:36:28 30 4
gpt4 key购买 nike

我想测量 jpeg 的白色/黄色量(在可调整的容差范围内)。

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

Almond Picture

最佳答案

Carl 的帖子已经解答了 99% 的问题,这里还有一些内容可以测量白色/接近白色的图像数量:

# 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

enter image description here

# 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:

enter image description 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

关于r - 测量 jpeg 中的空白,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25670005/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com