Closed bhive01 closed 7 years ago
A few examples of the images. They are RVB/RGB and are not completely masks thanks to a yellow outline and a red midline.
@vbonhomme, Yes the lines were the issue. These images are "false color" outputs from ImageJ that show the outline of the fruit and the midline estimate (not always great, see last fruit). I was hoping to not have to run my macro again (because it takes hours) so I wrote a simple python/openCV script to find the largest object, fill it in and output that. Then Momoc::read.jpg() was able to complete.
Thanks for your help.
import argparse
import sys
import os.path
import trans #pip install trans
import time
import datetime
import cv2
import math
import numpy as np
# set up parser to take input of folder with images to loop through
ap = argparse.ArgumentParser()
ap.add_argument("--path", help = "Path to folder containing images")
args = ap.parse_args()
# create new folder for storing images (resolution to minute so can overwrite if you start two processes quickly after each other)
currentTime = datetime.datetime.now().strftime('%Y-%m-%d_%H-%M-%S')
foldername = '{0}_BWimages'.format(currentTime)
savedir = os.path.join(args.path, foldername)
os.makedirs(savedir)
#define types of tiles we're interested in
filetypes = tuple([".JPG", ".jpg", ".JPEG", ".jpeg"])
# read the filelist from the path directory
filelist = [f for f in os.listdir(args.path) if f.endswith(filetypes)]
# how long is the list of files for progress reporting
listlength = len(filelist)
for fileindex, filename in enumerate(filelist):
# progress bar
pctdone = int(float(fileindex)/float(listlength) * 100)
sys.stdout.write("\rPercentDone: %d%%" % pctdone)
sys.stdout.flush()
# read in image
img = cv2.imread(os.path.join(args.path, filename))
# get image attributes
height, width, channels = img.shape
# create gray image for further processing
gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
#slight blur and then otsu thresholding
gaussian = cv2.GaussianBlur(gray_img,(5,5),0)
ret, threshold = cv2.threshold(gaussian, 0, 255, cv2.THRESH_BINARY+cv2.THRESH_OTSU)
# invert image to find contours
des = cv2.bitwise_not(threshold)
#find contours
_, contours, hierarchy = cv2.findContours(des, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
# https://stackoverflow.com/questions/25552765/python-opencv-second-largest-object
areaArray = []
#fill all contours in
for i, c in enumerate(contours):
area = cv2.contourArea(c)
areaArray.append(area)
#first sort the array by area
sorteddata = sorted(zip(areaArray, contours), key=lambda x: x[0], reverse=True)
#find the nth largest contour [n-1][1], in this case 2
largestcontour = sorteddata[0][1]
out_im = np.zeros((height, width, 1), np.uint8)
out_im = cv2.drawContours(out_im, [largestcontour], -1, 255, cv2.FILLED) # set everything to white inside all contours
out_im = (255-out_im)
#save out thresholded and filled in masks
savefilename = '{0}'.format(filename)
savefilecomplete = os.path.join(savedir, savefilename)
cv2.imwrite(savefilecomplete, out_im, [int(cv2.IMWRITE_JPEG_QUALITY), 90])
I have outlines of fruit that I'm trying to import, but for some reason it keeps getting stuck seemingly randomly during the import process.
Sometimes it stops after just a few, other times after more than 100, but after 45 minutes of trying it has not completed the import fully.
When it gets stuck, one core is stuck at 100% usage and no further updates happen. I can cancel with an 'esc' so it doesn't lock R completely.
How can I pin down what it is doing to help get a fix? CC: @DanChitwood