Public
Edited
Dec 16, 2023
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
apple_result = {
// Sampled middle-most colors of each image
const colors = [[179,63,48],[167,207,103],[252,219,3],[44,42,66]]
const curr = Number(apple[6])-1

// Computing max distance excluding a selected apple
var max = -1
for (let i = 0; i < colors.length; i++) {
if (i != curr){
for (let j = 0; j < colors.length; j++) {
if (j != curr && j != i) {
const val = (colors[i][0]-colors[j][0])**2 + (colors[i][1]-colors[j][1])**2 + (colors[i][2]-colors[j][2])**2
if (val > max){
max = val
}
}
}
}
}
// Checking whether or not the test apple falls within the range
for (let i = 0; i < colors.length; i++){
const val = (colors[i][0]-colors[curr][0])**2 + (colors[i][1]-colors[curr][1])**2 + (colors[i][2]-colors[curr][2])**2

if (val > max){
return "Apple "+(curr+1)+" is not an apple."
}
}

return "Apple "+(curr+1)+" is an apple."

}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
{
const image = await FileAttachment("splay_test6@1.jpeg").image()
const canvas = DOM.canvas(image.width, image.height);
const data = cv.imread(image);
cv.cvtColor(data, data, cv.COLOR_RGB2HLS);

const ksize = {width : 2, height : 2};

var rgb = hexToRgb(color)

// NOTE THESE VALUES ARE BGR NOT RGB
// Lower-bound Skin Color
const l_skin = cv.matFromArray(1,3, cv.CV_8UC1, [0, 30, 50])

// Upper-bound Skin Color
const u_skin = cv.matFromArray(1,3, cv.CV_8UC1, [rgb.b,rgb.g+30,rgb.r+30])

// Computing skin threshold
const skin_threshold = new cv.Mat();

// Selecting all pixels in that threshold
cv.inRange(data, l_skin, u_skin,data)

// Blurring the hand
cv.blur(data,data, ksize)
cv.imshow(canvas,data)
data.delete();

return sideBySide(image,canvas)
}
Insert cell
Insert cell
Insert cell
Insert cell
{
const image = await FileAttachment("splay_test6@1.jpeg").image()
const canvas = DOM.canvas(image.width, image.height);

const img = cv.imread(image);
const data = img.clone();
cv.cvtColor(data, data, cv.COLOR_RGB2HLS);

const ksize = {width : 2, height : 2};

var rgb = hexToRgb(color)

// NOTE THESE VALUES ARE BGR NOT RGB
// Lower-bound Skin Color
const l_skin = cv.matFromArray(1,3, cv.CV_8UC1, [0, 30, 50])

// Upper-bound Skin Color
const u_skin = cv.matFromArray(1,3, cv.CV_8UC1, [165,218,246])

// Computing skin threshold
const skin_threshold = new cv.Mat();

// Selecting all pixels in that threshold
cv.inRange(data, l_skin, u_skin,data)

// Blurring the hand
cv.blur(data,data, ksize)

// ----------------------- NEW -----------------------

const offset = cv.Point(0,0)

let contours = new cv.MatVector();
let hierarchy = new cv.Mat();
// Extracting the contours
cv.findContours(data,contours,hierarchy,cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE)

// Drawing them on photo for visual representation
cv.drawContours(img, contours, -1, [255,0,0,255], 10, cv.LINE_8, hierarchy, 100)
cv.imshow(canvas,img)
data.delete();

return sideBySide(image,canvas)
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more