Showing posts with label Ask and Answer. Show all posts
Showing posts with label Ask and Answer. Show all posts

Thursday, 12 January 2023

 Buffer Data Into Image Using Memcopy-OpenCV

 Buffer Data Convert as Image Using OpenCV


                                          Code
Code version v0.1
---------------------------------------------------------------------------------------------------------------------------


#include <tchar.h>
#include <stdio.h>
#include <strsafe.h>

HANDLE hSlot;
LPCTSTR SlotName = TEXT("\\\\.\\mailslot\\mailslot");

BOOL ReadSlot()
{
    DWORD cbMessage, cMessage, cbRead;
    BOOL fResult;
    LPTSTR lpszBuffer;
    TCHAR achID[80];
    DWORD cAllMessages;
    HANDLE hEvent;
    OVERLAPPED ov;

    cbMessage = cMessage = cbRead = 0;

    hEvent = CreateEvent(NULL, FALSE, FALSE, TEXT("ExampleSlot"));
    if (NULL == hEvent)
        return FALSE;
    ov.Offset = 0;
    ov.OffsetHigh = 0;
    ov.hEvent = hEvent;

    fResult = GetMailslotInfo(hSlot, // mailslot handle
        (LPDWORD)NULL,               // no maximum message size
        &cbMessage,                   // size of next message
        &cMessage,                    // number of messages
        (LPDWORD)NULL);              // no read time-out

    if (!fResult)
    {
        printf("GetMailslotInfo failed with %d.\n", GetLastError());
        return FALSE;
    }

    if (cbMessage == MAILSLOT_NO_MESSAGE)
    {
        printf("Waiting for a message...\n");
        return TRUE;
    }

    cAllMessages = cMessage;

    while (cMessage != 0)  // retrieve all messages
    {
        // Allocate memory for the message.

        lpszBuffer = (LPTSTR)GlobalAlloc(GPTR,
            lstrlen((LPTSTR)achID) * sizeof(TCHAR) + cbMessage);
        if (NULL == lpszBuffer)
            return FALSE;
        lpszBuffer[0] = '\0';

        fResult = ReadFile(hSlot,
            lpszBuffer,
            cbMessage,
            &cbRead,
            &ov);

        if (!fResult)
        {
            printf("ReadFile failed with %d.\n", GetLastError());
            GlobalFree((HGLOBAL)lpszBuffer);
            return FALSE;
        }
        cv::Mat mat = cv::Mat(640, 640, CV_8UC3, cv::Scalar(0, 0, 0));
        std::memcpy(mat.data, lpszBuffer, 640 * 640 * 3);
      
        cv::imshow("YOURWINDOW", mat);
        cv::waitKey(1);

        GlobalFree((HGLOBAL)lpszBuffer);

        fResult = GetMailslotInfo(hSlot,  // mailslot handle
            (LPDWORD)NULL,               // no maximum message size
            &cbMessage,                   // size of next message
            &cMessage,                    // number of messages
            (LPDWORD)NULL);              // no read time-out

        if (!fResult)
        {
            printf("GetMailslotInfo failed (%d)\n", GetLastError());
            return FALSE;
        }
    }
    CloseHandle(hEvent);
    return TRUE;
}


BOOL WINAPI MakeSlot(LPCTSTR lpszSlotName)
{
    hSlot = CreateMailslot(
        lpszSlotName,
        0,
        MAILSLOT_WAIT_FOREVER,
        (LPSECURITY_ATTRIBUTES)NULL);

    if (hSlot == INVALID_HANDLE_VALUE) {
        printf("CreateMailslot failed with %d\n", GetLastError());
        return FALSE;
    }
    else
        std::cout << "Mailslot created successfully.\n";
    return TRUE;
}

int main()
{
    MakeSlot(SlotName);


    cv::namedWindow("YOURWINDOW", cv::WINDOW_AUTOSIZE);
    while (true) {
        ReadSlot();
    }
}

// producer:

LPCTSTR SlotName = TEXT("\\\\.\\mailslot\\mailslot");

BOOL WriteSlot(HANDLE hSlot, LPCTSTR lpszMessage)
{
    BOOL fResult;
    DWORD cbWritten;

    fResult = WriteFile(hSlot, lpszMessage,
                (DWORD)(lstrlen(lpszMessage) + 1) * sizeof(TCHAR),
                &cbWritten, (LPOVERLAPPED)NULL);

    if (!fResult) {
        printf("WriteFile failed with %d.\n", GetLastError());
        return FALSE;
    }

    printf("Slot written to successfully.\n");

    return TRUE;
}
cv::Mat mat =
        cv::imread("C:\\Repos\\tmp\\decode\\images\\output_0664.jpg");
            size_t sizeInBytes = mat.step[0] * mat.rows;

            LPVOID lpBuffer = malloc(sizeInBytes);

            HANDLE hFile;

            hFile = CreateFile(SlotName, GENERIC_WRITE,
                       FILE_SHARE_READ,
                       (LPSECURITY_ATTRIBUTES)NULL,
                       OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,
                       (HANDLE)NULL);

            if (hFile == INVALID_HANDLE_VALUE) {
                printf("CreateFile failed with %d.\n",
                       GetLastError());
                return FALSE;
            }
            DWORD bytesWritten;
            memcpy(lpBuffer, &mat.data[0], sizeInBytes);

            WriteFile(hFile, lpBuffer, sizeInBytes, &bytesWritten,
                  NULL);

            CloseHandle(hFile);
 
 
 
 
 
Code Version V0.2:
-----------------------------------------------------------------------------------------------


#include <tchar.h>
#include <stdio.h>
#include <strsafe.h>

HANDLE hSlot;
LPCTSTR SlotName = TEXT("\\\\.\\mailslot\\mailslot");

BOOL ReadSlot()
{
    DWORD cbMessage, cMessage, cbRead;
    BOOL fResult;
    LPTSTR lpszBuffer;
    TCHAR achID[80];
    DWORD cAllMessages;
    HANDLE hEvent;
    OVERLAPPED ov;

    cbMessage = cMessage = cbRead = 0;

    hEvent = CreateEvent(NULL, FALSE, FALSE, TEXT("ExampleSlot"));
    if (NULL == hEvent)
        return FALSE;
    ov.Offset = 0;
    ov.OffsetHigh = 0;
    ov.hEvent = hEvent;

    fResult = GetMailslotInfo(hSlot, // mailslot handle
        (LPDWORD)NULL,               // no maximum message size
        &cbMessage,                   // size of next message
        &cMessage,                    // number of messages
        (LPDWORD)NULL);              // no read time-out

    if (!fResult)
    {
        printf("GetMailslotInfo failed with %d.\n", GetLastError());
        return FALSE;
    }

    if (cbMessage == MAILSLOT_NO_MESSAGE)
    {
        printf("Waiting for a message...\n");
        return TRUE;
    }

    cAllMessages = cMessage;

    while (cMessage != 0)  // retrieve all messages
    {
        // Allocate memory for the message.

        lpszBuffer = (LPTSTR)GlobalAlloc(GPTR,
            lstrlen((LPTSTR)achID) * sizeof(TCHAR) + cbMessage);
        if (NULL == lpszBuffer)
            return FALSE;
        lpszBuffer[0] = '\0';

        fResult = ReadFile(hSlot,
            lpszBuffer,
            cbMessage,
            &cbRead,
            &ov);

        if (!fResult)
        {
            printf("ReadFile failed with %d.\n", GetLastError());
            GlobalFree((HGLOBAL)lpszBuffer);
            return FALSE;
        }
        cv::Mat mat = cv::Mat(640, 640, CV_8UC3, cv::Scalar(0, 0, 0));
        std::memcpy(mat.data, lpszBuffer, 640 * 640 * 3);
      
        cv::imshow("YOURWINDOW", mat);
        cv::waitKey(1);

        GlobalFree((HGLOBAL)lpszBuffer);

        fResult = GetMailslotInfo(hSlot,  // mailslot handle
            (LPDWORD)NULL,               // no maximum message size
            &cbMessage,                   // size of next message
            &cMessage,                    // number of messages
            (LPDWORD)NULL);              // no read time-out

        if (!fResult)
        {
            printf("GetMailslotInfo failed (%d)\n", GetLastError());
            return FALSE;
        }
    }
    CloseHandle(hEvent);
    return TRUE;
}


BOOL WINAPI MakeSlot(LPCTSTR lpszSlotName)
{
    hSlot = CreateMailslot(
        lpszSlotName,
        0,
        MAILSLOT_WAIT_FOREVER,
        (LPSECURITY_ATTRIBUTES)NULL);

    if (hSlot == INVALID_HANDLE_VALUE) {
        printf("CreateMailslot failed with %d\n", GetLastError());
        return FALSE;
    }
    else
        std::cout << "Mailslot created successfully.\n";
    return TRUE;
}

int main()
{
    MakeSlot(SlotName);


    cv::namedWindow("YOURWINDOW", cv::WINDOW_AUTOSIZE);
    while (true) {
        ReadSlot();
    }
}

// producer:

LPCTSTR SlotName = TEXT("\\\\.\\mailslot\\mailslot");

BOOL WriteSlot(HANDLE hSlot, LPCTSTR lpszMessage)
{
    BOOL fResult;
    DWORD cbWritten;

    fResult = WriteFile(hSlot, lpszMessage,
                (DWORD)(lstrlen(lpszMessage) + 1) * sizeof(TCHAR),
                &cbWritten, (LPOVERLAPPED)NULL);

    if (!fResult) {
        printf("WriteFile failed with %d.\n", GetLastError());
        return FALSE;
    }

    printf("Slot written to successfully.\n");

    return TRUE;
}
cv::Mat mat =
        cv::imread("C:\\Repos\\tmp\\decode\\images\\output_0664.jpg");
            size_t sizeInBytes = mat.step[0] * mat.rows;

            LPVOID lpBuffer = malloc(sizeInBytes);

            HANDLE hFile;

            hFile = CreateFile(SlotName, GENERIC_WRITE,
                       FILE_SHARE_READ,
                       (LPSECURITY_ATTRIBUTES)NULL,
                       OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,
                       (HANDLE)NULL);

            if (hFile == INVALID_HANDLE_VALUE) {
                printf("CreateFile failed with %d.\n",
                       GetLastError());
                return FALSE;
            }
            DWORD bytesWritten;
            memcpy(lpBuffer, &mat.data[0], sizeInBytes);

            WriteFile(hFile, lpBuffer, sizeInBytes, &bytesWritten,
                  NULL);

            CloseHandle(hFile); 
 
 
 

Tuesday, 26 January 2021

Image Instant Segmentation:

                  Image Instant Segmentation


Image Segmentation instantly with 3 Line of codes:

Image Segmentation:


import pixellib

from pixellib.semantic import semantic_segmentation

import cv2


segment_image = semantic_segmentation()

segment_image.load_pascalvoc_model("pascal.h5")

output, segmap = segment_image.segmentAsPascalvoc("Testimage.jpg")

cv2.imwrite("img.jpg", output)

print(output.shape)

step 2:

        

import pixellib

from pixellib.semantic import semantic_segmentation

import cv2


segment_image = semantic_segmentation()

segment_image.load_pascalvoc_model("pascal.h5")

segmap, segoverlay = segment_image.segmentAsPascalvoc("Testimage.jpg", overlay= True)

cv2.imwrite("img.jpg", segoverlay)

print(segoverlay.shape)


Mask r-cnn:

code:

import pixellib

from pixellib.instance import instance_segmentation

segment_image = instance_segmentation()

segment_image.load_model("mask_rcnn_coco.h5") 

segment_image.segmentImage("path_to_image", output_image_name = "output_image_path")


Step 2:

import pixellib

from pixellib.instance import instance_segmentation

import cv2


instance_seg = instance_segmentation()

instance_seg.load_model("mask_rcnn_coco.h5")

segmask, output = instance_seg.segmentImage("sample2.jpg")

cv2.imwrite("img.jpg", output)

print(output.shape)



Installation Prcoedure:

1.Python 3.6

2.pixellib

3.Openc 3.4.12






Thursday, 22 November 2018

                   Floor Detection Application

Detect and display the floor using Live Images:

Demo Output:

Input Images Used:



Output Images:

AI Model Segmented Images:



                                        RND using MATLAB Programming

MATLAB Code start here

clc

clear all

close all;

% [a,b]=uigetfile('*.jpg','Segmented    1   image');

rgb=imread('\correct\10r.jpg');

figure,imshow(rgb) 

title('Segmented image')

%  Threshold 

% Pixel 143 71 111 %% in gray 97

gray=rgb2gray(rgb);

figure,imshow(gray) 

title('Segmented grayscale')

hl=gray<97;

hl=find(hl==1);

sz=size(gray)

mask=zeros(sz);

mask(hl)=255;

figure,imshow(mask)

title('binary image')

BW2 = bwareaopen(mask, 2945);

figure,imshow(BW2)

% se = strel('ball',2,5);

% se = strel('disk',2);

% afterOpening = imopen(mask,se);

% se = strel('disk',11);        

% se2 = strel('line',3,90)

se = strel('disk',2);        

se2 = strel('line',3,90)

IM2 = imdilate(BW2,se);

figure,imshow(IM2)

title('smoothed edge')

hls=find(IM2==1);

nohls=find(IM2==0);

edg_img=edge(IM2,'Canny');

figure,imshow(edg_img)

title('coordinate edge')

% [a1,b]=uigetfile('*.jpg','original');

% original=imread('correct\2.jpg');

% figure,imshow(original)

floor_img=imread('\tl2.jpg');

original=rgb;

red_l=original(:,:,1);

green_l=original(:,:,2);

blue_l=original(:,:,3);

red_l(hls)=255;

green_l(hls)=0.22;

blue_l(hls)=0.33;

seg=cat(3,red_l,green_l,blue_l);

figure,imshow(seg)

%% sheet read

% [ax,vx]=uigetfile('*.jpg');

% sheet=imread(ax);

sheet=imread('\tl2.jpg');

sh=size(rgb);

resz=imresize(sheet,sh(1:2));

r_resz=resz(:,:,1);

g_resz=resz(:,:,2);

b_resz=resz(:,:,3);

r_resz(nohls)=0;

g_resz(nohls)=0;

b_resz(nohls)=0;

recn=cat(3,r_resz,g_resz,b_resz);

red_l(hls)=0;

green_l(hls)=0;

blue_l(hls)=0;

figure,imshow(r_resz)

figure,imshow(red_l)

r1=imfuse(red_l,r_resz,'blend','Scaling','joint');

g1=imfuse(green_l,g_resz,'blend','Scaling','joint');

b1=imfuse(blue_l,b_resz,'blend','Scaling','joint');

recon=cat(3,r1,g1,b1);

figure,imshow(recon*2)

imwrite(recon*2,'rechanged8.jpg')

figure,imshow(imfuse(red_l,r_resz,'blend','Scaling','joint'))


                       Application Development using Python

Packages Installation:

1.Python 3.6 

Packages:

pip install opencv-contrib-python=3.4.2.17

pip install pixellib

pip intall numpy

Module run:

Input parameters:

# model_root=r'.hf'  ## model root

# input_imgdir='' ## Input Image root

# output_imgdir= '' ## Segment root

# sheet_image='' ## Floor sheet image root 

# Smoothed_root='' # Final input root

How to use Packages Import ?

import floorgui

import imgmerg

image_segment(model_root,input_imgdir,output_imgdir,sheet_image,Smoothed_root)

How to Run:

Use Test.py-----Testing 

Terminal : python Test.py

Model Download Link:

https://drive.google.com/file/d/1q6dZmlc6_H-B8i-yS_fGsXVQ8lFjy3zw/view?usp=sharing

Source Code Link: