Code Monkey home page Code Monkey logo

Comments (1)

aryan-at-ul avatar aryan-at-ul commented on June 12, 2024

` def make_graph_from_image(image,dirs_names,class_name,data_typ,image_name):

G2 = nx.Graph()
print("image shape ",image.shape,"image name ",image_name)
print("class name ",dirs_names[-2]) # catrgory
print("data type ",dirs_names[-3]) # traintes val
print("image name ",dirs_names)
print("image name ",image_name) #use this
dirname = dirs_names[-2]

target = 0 
if dirname == "tumor":
    target = 1

s = time.time()
if len(image.shape) < 3:
    image = np.stack((image,)*3, axis=-1)

if image.shape[2] > 3:
    image = image[:, :, :3]
print(image.shape,"here here ") 
segments,_ = DISF_Superpixels(image, 8000, 50) 
rag = graph.rag_mean_color(image, segments,mode='distance')
out = color.label2rgb(segments, image, kind='avg')
out = segmentation.mark_boundaries(image, segments, (0, 0, 0))
e = time.time()
print(f"segmentation time = {e-s}")
seg_imgs = []
node_class = {}
start_time = time.time()
for (i, segVal) in enumerate(np.unique(segments)):

    mask = np.zeros(image.shape[:2], dtype = "uint8")
    
    mask[segments == segVal] = 255
    segimg = cv2.cvtColor(cv2.bitwise_and(image, image, mask = mask), cv2.COLOR_BGR2RGB)
    segimg = cv2.bitwise_and(image, image, mask = mask)

    result = np.zeros_like(image)
    result[segments == i] = image[segments == i]

    gray = cv2.cvtColor(segimg, cv2.COLOR_BGR2GRAY)
    thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_OTSU + cv2.THRESH_BINARY)[1]
    # Find contour and sort by contour area
    cnts = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
    cnts = cnts[0] if len(cnts) == 2 else cnts[1]
    cnts = sorted(cnts, key=cv2.contourArea, reverse=True)
    seg = segimg.copy()
    for c in cnts:
        x,y,w,h = cv2.boundingRect(c)
        seg = image[y:y+h, x:x+w]
        break
    seg = cv2.cvtColor(seg, cv2.COLOR_BGR2RGB)
    rag.nodes[segVal]['image'] = seg
    seg_imgs.append([seg,segVal])


with concurrent.futures.ThreadPoolExecutor(max_workers=30) as executor:
    futures = [executor.submit(fet_from_img, seg_img[0], seg_img[1])  for  i,seg_img in enumerate(seg_imgs)]
    for future in concurrent.futures.as_completed(futures):
        try:
            img_fet,i = future.result()
            print("this result here",i)
            G2.add_node(i, x = img_fet)
        except Exception as exc:
            print(f'generated an exception: {exc} for seg {i}')
            print(traceback.format_exc())
            continue

end_time = time.time()
print(f"{image_name} total time take per image is {end_time - start_time}")
edges = rag.edges
for e in edges:
    G2.add_weighted_edges_from([(e[0],e[1],rag[e[0]][e[1]]['weight'])])

return G2,segments` 

this is the part that converts image to graph, this is one is using DISF segments,_ = DISF_Superpixels(image, 8000, 50) you can replace this with SLIC segmentation from skimage, it creates segment and then creates a rag and then later using each segment assign features as node features for each segement.

from aics_2023_submission.

Related Issues (1)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.