精华内容
下载资源
问答
  • Python_OpenCV_灰度拉伸

    2021-03-17 09:17:41
    灰度拉伸 output=255/(B-A)*[img_gray-A] 其中,A为最小灰度级,B为最大灰度级 img_gray为输入图像,output为输出图像 缺点:若图像中最小灰度值=0,最大灰度值=255,则图像不会有什么改变 ''' import cv2 ...

     

    '''
    灰度拉伸
    output=255/(B-A)*[img_gray-A]
    其中,A为最小灰度级,B为最大灰度级
                  img_gray为输入图像,output为输出图像
    
    缺点:若图像中最小灰度值=0,最大灰度值=255,则图像不会有什么改变
    '''
     
    import cv2
    import numpy as np
    
    def grey_scale(image):
        img_gray=cv2.cvtColor(image,cv2.COLOR_RGB2GRAY)
    
        rows,cols=img_gray.shape
        flat_gray=img_gray.reshape((cols*rows,)).tolist()
        A=min(flat_gray)
        B=max(flat_gray)
        print("最小灰度值:%d,最大灰度值:%d"%(A,B))
        output=np.uint8(255/(B-A)*(img_gray-A)+0.5)
        return output
    
    input=cv2.imread("test.jpg")
    out=grey_scale(input)
    
    cv2.namedWindow(u"in",0)#可以拖动窗口大小
    cv2.resizeWindow(u"in", 640, 480)#设置窗口大小
    cv2.moveWindow(u"in",1200,500)#设置窗口位置
    
    cv2.namedWindow(u"out",0)#可以拖动窗口大小
    cv2.resizeWindow(u"out", 640, 480)#设置窗口大小
    cv2.moveWindow(u"out",1200,500)#设置窗口位置
    
    cv2.imshow("in",cv2.cvtColor(input,cv2.COLOR_BGR2GRAY))
    cv2.imshow("out",out)
    
    cv2.waitKey(0)
    cv2.destroyAllWindows()

     

    效果:

    展开全文
  • Python OpenCV实例:图像灰度拉伸

    千次阅读 2018-04-19 17:13:00
    灰度拉伸 定义:灰度拉伸,也称对比度拉伸,是一种简单的线性点运算。作用:扩展图像的 直方图,使其充满整个灰度等级范围内 公式: g(x,y) = 255 / (B - A) * [f(x,y) - A], 其中,A = min[f(x,y)],最...
    #coding:utf-8
    '''
    灰度拉伸
    定义:灰度拉伸,也称对比度拉伸,是一种简单的线性点运算。作用:扩展图像的
          直方图,使其充满整个灰度等级范围内
    公式:
    g(x,y) = 255 / (B - A) * [f(x,y) - A],
    其中,A = min[f(x,y)],最小灰度级;B = max[f(x,y)],最大灰度级;
         f(x,y)为输入图像,g(x,y)为输出图像
    
    缺点:如果灰度图像中最小值A=0,最大值B=255,则图像没有什么改变
    
    '''
    
    import cv2
    import numpy as np
    
    def grey_scale(image):
        img_gray = cv2.cvtColor(image,cv2.COLOR_RGB2GRAY)
        
        rows,cols = img_gray.shape
        flat_gray = img_gray.reshape((cols * rows,)).tolist()
        A = min(flat_gray)
        B = max(flat_gray)
        print('A = %d,B = %d' %(A,B))
        output = np.uint8(255 / (B - A) * (img_gray - A) + 0.5)
        return output
    
    src = cv2.imread('datas/f4.jpg')
    result = grey_scale(src)
    cv2.imshow('src',cv2.cvtColor(src,cv2.COLOR_BGR2GRAY))
    cv2.imshow('result',result)
    
    cv2.waitKey()
    cv2.destroyAllWindows()

    171256_khyh_106657.png

     

    展开全文
  • 2019独角兽企业重金招聘Python工程师标准>>> ...
    #if !defined(AFX_WAVEDLG_H__3AD6300B_435E_4773_8C88_DDE56BEE2A52__INCLUDED_)
    #define AFX_WAVEDLG_H__3AD6300B_435E_4773_8C88_DDE56BEE2A52__INCLUDED_
    
    #if _MSC_VER > 1000
    #pragma once
    #endif // _MSC_VER > 1000
    
    /
    // CWaveDlg dialog
    
    class CWaveDlg : public CDialog
    {
    // Construction
    public:
    CWaveDlg(CWnd* pParent = NULL);	// standard constructor
    
    // Dialog Data
    //{{AFX_DATA(CWaveDlg)
    enum { IDD = IDD_WAVE_DIALOG };
    CSliderCtrl	m_slider2;
    CSliderCtrl	m_sliderCtr;
    //}}AFX_DATA
    
    // ClassWizard generated virtual function overrides
    //{{AFX_VIRTUAL(CWaveDlg)
    protected:
    virtual void DoDataExchange(CDataExchange* pDX);	// DDX/DDV support
    //}}AFX_VIRTUAL
    
    
    // Implementation
    protected:
    HICON m_hIcon;
    
    // Generated message map functions
    //{{AFX_MSG(CWaveDlg)
    
    virtual BOOL OnInitDialog();
    afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
    afx_msg void OnPaint();
    afx_msg HCURSOR OnQueryDragIcon();
    afx_msg void OnStaticWave();
    afx_msg void OnButtonTest();
    afx_msg void OnDestroy();
    
    afx_msg HBRUSH OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor);
    afx_msg void OnCancelMode();
    afx_msg void OnTimer(UINT nIDEvent);
    virtual void OnOK();
    afx_msg void OnButton2();
    afx_msg void OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar);
    afx_msg void OnMenuitemRgbToGray();
    
    afx_msg void OnMenuitemOrgImag();
    afx_msg void OnMenuitemBinary();
    afx_msg void OnButtonScrollWnd();
    afx_msg void OnMenuitemInvertY();
    afx_msg void OnMenuitemInvertX();
    afx_msg void OnMenuitemHisto();
    afx_msg void OnMenuitemSaveGray8bit();
    afx_msg void OnMenuitemSaveBinaryBmp1bit();
    afx_msg void OnMenuitemGlobGrayStretch();
    afx_msg void OnMenuitemHistoEqualization();
    
    //}}AFX_MSG
    DECLARE_MESSAGE_MAP()
    private:
    double m_dbPercent;
    byte m_bBrightness;
    int m_nPoolWid;
    int m_nPoolHei;
    CBitmap m_bmp,m_newBmp;
    int * m_pPoolBuff,*m_pPoolBuff1;
    byte *m_pImageDateBuff,*m_pImageDateBuff1;
    CDC* m_pDC;
    double GetDistance(const CPoint & p1,const CPoint &p2);
    int GetAmplitude(int a0,int a1,int a2,int a3,int a4);
    int GetEnergyFromWaterSourece(const CPoint &ptCenter,const CPoint &pt,double radius,int energy=50);
        CBrush m_brush;
    void Shocking();
    void RefreshImag(void);
    void AdjustContrastAndBrightness(void);//调节对比度亮度
    INT m_nCount;
    
    void LimitMaxMin(byte& val)
    {
    if (val>255)
    {
    val =255;
    }
    if (val<0)
    {
    val= 0;
    }
    }
    
    void AvgChannels(int & r,int &g,int &b);
    void AdjustSimpleContrastAndBrightness(void);//调节对比度亮度
    void Rgb2Gray24(int typeFlag);
    void Gray2Binary(byte* imageBuffer,byte threshold);
    void SaveBmp(void);
        void SaveBmpSingle(void);//8bit单色位图
        void SaveBmpBinary(void);//1bit单色位图
    void InvertBuffer(INT direction);
    void RoateImag(CPoint & roatePos,double roateAngle);
    void GetHistogram(int *Histo);
    void DrawHistoGram(int *Histo);
        void Rgb2Gray8(void);
    void Rgb2Gray16(void);
    void Rgb2Rgb8(void);
    void Rgb2Rgb16(void);
    void GlobGrayStrench();//全局灰度直方图拉伸
        void HistogramEqualization(void);//直方图均衡化
        void CalculateCDF(double *p);//计算累计分布函数
    };
    
    //{{AFX_INSERT_LOCATION}}
    // Microsoft Visual C++ will insert additional declarations immediately before the previous line.
    
    #endif // !defined(AFX_WAVEDLG_H__3AD6300B_435E_4773_8C88_DDE56BEE2A52__INCLUDED_)
    
    
    
    #include "stdafx.h"
    #include "Wave.h"
    #include "WaveDlg.h"
    #include<MATH.H>
    #ifdef _DEBUG
    #define new DEBUG_NEW
    #undef THIS_FILE
    static char THIS_FILE[] = __FILE__;
    #endif
    
    CWaveDlg::CWaveDlg(CWnd* pParent /*=NULL*/)
    : CDialog(CWaveDlg::IDD, pParent)
    {
    //{{AFX_DATA_INIT(CWaveDlg)
    //}}AFX_DATA_INIT
    // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
    m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
    m_dbPercent=0.0;
    m_nCount=1;
    int len=6;
    while (len--)
    {
    int a=len;
    }
    }
    
    void CWaveDlg::DoDataExchange(CDataExchange* pDX)
    {
    CDialog::DoDataExchange(pDX);
    //{{AFX_DATA_MAP(CWaveDlg)
    DDX_Control(pDX, IDC_SLIDER2, m_slider2);
    DDX_Control(pDX, IDC_SLIDER, m_sliderCtr);
    //}}AFX_DATA_MAP
    }
    
    BEGIN_MESSAGE_MAP(CWaveDlg, CDialog)
    //{{AFX_MSG_MAP(CWaveDlg)
    ON_WM_SYSCOMMAND()
    ON_WM_PAINT()
    ON_WM_QUERYDRAGICON()
    ON_BN_CLICKED(IDC_STATIC_WAVE, OnStaticWave)
    ON_BN_CLICKED(IDC_BUTTON_TEST, OnButtonTest)
    ON_WM_DESTROY()
    ON_WM_CTLCOLOR()
    ON_WM_CANCELMODE()
    ON_WM_TIMER()
    
    ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
    ON_WM_HSCROLL()
    ON_COMMAND(ID_MENUITEM_RGB_TO_GRAY, OnMenuitemRgbToGray)
    ON_COMMAND(ID_MENUITEM_ORG_IMAG, OnMenuitemOrgImag)
    ON_COMMAND(ID_MENUITEM_BINARY, OnMenuitemBinary)
    ON_BN_CLICKED(IDC_BUTTON_SCROLL_WND, OnButtonScrollWnd)
    ON_COMMAND(ID_MENUITEM_INVERT_Y, OnMenuitemInvertY)
    ON_COMMAND(ID_MENUITEM_INVERT_X, OnMenuitemInvertX)
    
    ON_COMMAND(ID_MENUITEM_HISTO, OnMenuitemHisto)
    ON_COMMAND(ID_MENUITEM_SAVE_GRAY_8BIT, OnMenuitemSaveGray8bit)
    ON_COMMAND(ID_MENUITEM_SAVE_BINARY_BMP_1BIT, OnMenuitemSaveBinaryBmp1bit)
    ON_COMMAND(ID_MENUITEM_GLOB_GRAY_STRETCH, OnMenuitemGlobGrayStretch)
    ON_COMMAND(ID_MENUITEM_HISTO_EQUALIZATION, OnMenuitemHistoEqualization)
    //}}AFX_MSG_MAP
    END_MESSAGE_MAP()
    
    BOOL CWaveDlg::OnInitDialog()
    {
    CDialog::OnInitDialog();
    
    // Add "About..." menu item to system menu.
    
    // IDM_ABOUTBOX must be in the system command range.
    ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
    ASSERT(IDM_ABOUTBOX < 0xF000);
    
    CMenu* pSysMenu = GetSystemMenu(FALSE);
    if (pSysMenu != NULL)
    
    {
    CString strAboutMenu;
    strAboutMenu.LoadString(IDS_ABOUTBOX);
    if (!strAboutMenu.IsEmpty())
    {
    pSysMenu->AppendMenu(MF_SEPARATOR);
    pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
    }
    }
    
    // Set the icon for this dialog.  The framework does this automatically
    //  when the application's main window is not a dialog
    SetIcon(m_hIcon, TRUE);	// Set big icon
    SetIcon(m_hIcon, FALSE);	// Set small icon
    
    // TODO: Add extra initialization here
    m_bmp.LoadBitmap(IDB_BITMAP);
    BITMAP bitMap;
    m_bmp.GetBitmap(&bitMap);
    m_nPoolWid=bitMap.bmWidth;
    m_nPoolHei=bitMap.bmHeight;
    
        GetDlgItem(IDC_STATIC_WAVE)->SetWindowPos(NULL,0,0,m_nPoolWid,m_nPoolHei,SWP_NOMOVE);
    m_pDC=GetDlgItem(IDC_STATIC_WAVE)->GetDC();
    int size=m_nPoolHei*m_nPoolWid;
        m_pPoolBuff=new int[size];
    m_pPoolBuff1=new int[size];
        memset(m_pPoolBuff,0,size*sizeof(int));
        memset(m_pPoolBuff1,0,size*sizeof(int));
    
        int ImageDateSize=size*bitMap.bmBitsPixel/8;
    m_pImageDateBuff=new byte[ImageDateSize];
     
    m_pImageDateBuff1=new byte[ImageDateSize];
    m_bmp.GetBitmapBits(ImageDateSize,m_pImageDateBuff);
    
    
    //m_newBmp.CreateCompatibleBitmap(m_pDC,m_nPoolWid,m_nPoolHei);
    m_newBmp.CreateBitmap(m_nPoolWid,m_nPoolHei,1,32,m_pImageDateBuff);
    //m_newBmp.CreateBitmapIndirect(&bitMap);
    //m_newBmp.CreateDiscardableBitmap(m_pDC,m_nPoolWid,m_nPoolHei);
    // 	//m_newBmp.SetBitmapBits(ImageDateSize,m_pImageDateBuff);
    m_brush.CreateSolidBrush(RGB(255,0,0));
    m_sliderCtr.SetRangeMax(100,TRUE);
    m_sliderCtr.SetRangeMin(0,TRUE);
    
    //m_sliderCtr.SetRangeMin(-100,TRUE);
    m_sliderCtr.SetPos(-100);
    m_sliderCtr.SetTicFreq(1);
    
    m_slider2.SetRangeMax(255,TRUE);
    m_slider2.SetRangeMin(0,TRUE);
    m_slider2.SetPos(0);
    m_slider2.SetTicFreq(5);
    return TRUE;  // return TRUE  unless you set the focus to a control
    
    }
    
    void CWaveDlg::OnSysCommand(UINT nID, LPARAM lParam)
    {
    if ((nID & 0xFFF0) == IDM_ABOUTBOX)
    {
    CAboutDlg dlgAbout;
    dlgAbout.DoModal();
    }
    else
    {
    CDialog::OnSysCommand(nID, lParam);
    }
    }
    
    void CWaveDlg::OnPaint() 
    {
    if (IsIconic())
    {
    CPaintDC dc(this); // device context for painting
    
    SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
    
    // Center icon in client rectangle
    int cxIcon = GetSystemMetrics(SM_CXICON);
    int cyIcon = GetSystemMetrics(SM_CYICON);
    CRect rect;
    GetClientRect(&rect);
    int x = (rect.Width() - cxIcon + 1) / 2;
    int y = (rect.Height() - cyIcon + 1) / 2;
    
    // Draw the icon
    dc.DrawIcon(x, y, m_hIcon);
    }
    else
    {
    CDialog::OnPaint();
    }
    }
    
    HCURSOR CWaveDlg::OnQueryDragIcon()
    {
    return (HCURSOR) m_hIcon;
    }
    
    
    void CWaveDlg::OnStaticWave() 
    {
    // TODO: Add your control notification handler code here
    
    CPoint ptCenter;
    ::GetCursorPos(&ptCenter);
    GetDlgItem(IDC_STATIC_WAVE)->ScreenToClient(&ptCenter);
        double radius=5.0;
        if (ptCenter.x<0||ptCenter.y<0||ptCenter.x>512||ptCenter.y>512)
        {
    return;
        }
    
    CPoint ptTmp;
    for (int j=0;j<m_nPoolHei;++j)
    {
    for (int i=0;i<m_nPoolWid;++i)
    {
    ptTmp.x=i;
    ptTmp.y=j;
    if (GetDistance(ptTmp,ptCenter)<=radius)
    {
    m_pPoolBuff[j*m_nPoolWid+i]=GetEnergyFromWaterSourece(ptCenter,ptTmp,radius,-500);
    }
    }
    }
    }
    
    void CWaveDlg::OnButtonTest() 
    {
     	
    }
    
    void CWaveDlg::Shocking()
    {
        OnStaticWave();
    int *pImageBuff=(int*)m_pImageDateBuff;
    int *pImageBuff1=(int*)m_pImageDateBuff1;
    for (int j=1;j<m_nPoolHei-1;++j)
    {
    for (int i=1;i<m_nPoolWid-1;++i)
    {
       int x1=m_pPoolBuff[j*m_nPoolWid+i-1];
               int x3=m_pPoolBuff[j*m_nPoolWid+i+1];
       int y1=m_pPoolBuff[(j-1)*m_nPoolWid+i];
               int y3=m_pPoolBuff[(j+1)*m_nPoolWid+i];
    
               m_pPoolBuff1[j*m_nPoolWid+i]=GetAmplitude(m_pPoolBuff1[j*m_nPoolWid+i],x1,x3,y1,y3);
       m_pPoolBuff1[j*m_nPoolWid+i]-=m_pPoolBuff1[j*m_nPoolWid+i]>>7;
       int newIndexX=x3-x1+i;
               int newIndexY=y3-y1+j;
               
       if (newIndexX>m_nPoolWid)
       {
       newIndexX=m_nPoolWid;
       }
       if (newIndexX<0)
       {
       newIndexX=0;
       }
    
       if (newIndexY>m_nPoolHei)
       {
       newIndexY=m_nPoolHei;
       }
       if (newIndexY<0)
       {
       newIndexY=0;
       }
    
               pImageBuff1[j*m_nPoolWid+i]=pImageBuff[m_nPoolWid*newIndexY+newIndexX];
    }
    }
    
        int * pTmp=m_pPoolBuff;
    m_pPoolBuff=m_pPoolBuff1;
        m_pPoolBuff1=pTmp;
        
    RefreshImag();
    
    }
    void CWaveDlg::RefreshImag(void)
    {
        m_newBmp.SetBitmapBits(m_nPoolWid*m_nPoolHei*4,m_pImageDateBuff1);	
        CDC memDC;
    memDC.CreateCompatibleDC(m_pDC);
    CBitmap *pOldBmp=memDC.SelectObject(&m_newBmp);
    m_pDC->BitBlt(0,0,m_nPoolWid,m_nPoolHei,&memDC,0,0,SRCCOPY);
    memDC.SelectObject(pOldBmp);
    memDC.DeleteDC();
    }
    double CWaveDlg::GetDistance(const CPoint & p1,const CPoint &p2)
    {
        return sqrt((p2.x-p1.x)*(p2.x-p1.x)+(p2.y-p1.y)*(p2.y-p1.y));
    }
    
    int CWaveDlg::GetAmplitude(int a0,int a1,int a2,int a3,int a4)
    {
    
     int a=((a1+a2+a3+a4)>>1)-a0;
     //a-=(a>>5);
     return a;
    }
    int CWaveDlg::GetEnergyFromWaterSourece(const CPoint &ptCenter,const CPoint &pt,double radius,int energy)
    {
    double dis=GetDistance(ptCenter,pt);
    return  (int)((1-dis/radius)*energy);
    
    }
    
    void CWaveDlg::OnDestroy() 
    {
    CDialog::OnDestroy();
    
    // TODO: Add your message handler code here
    GetDlgItem(IDC_STATIC_WAVE)->ReleaseDC(m_pDC);
    delete [] m_pImageDateBuff;
    delete [] m_pImageDateBuff1;
    delete [] m_pPoolBuff;
    delete [] m_pPoolBuff1;
    }
    bool m_b=false;
    
    HBRUSH CWaveDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) 
    {
    HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
    
    // TODO: Change any attributes of the DC here
    
    // TODO: Return a different brush if the default is not desired
    return hbr;
    }
    
    void CWaveDlg::OnCancelMode() 
    {
    CDialog::OnCancelMode();
    
    // TODO: Add your message handler code here
    
    }
    
    void CWaveDlg::OnTimer(UINT nIDEvent) 
    {
    // TODO: Add your message handler code here and/or call default
    Shocking();
    CDialog::OnTimer(nIDEvent);
    }
    
    void CWaveDlg::OnOK() 
    {
    // TODO: Add extra validation here
    //	CDialog::OnOK();
    //SetTimer(1,40,NULL);
    }
    
    void CWaveDlg::OnButton2() 
    {
    // TODO: Add your control notification handler code here
    
    }
    
    void CWaveDlg::AvgChannels(int & r,int &g,int &b)
    {
    for (int j=0;j<m_nPoolHei;++j)
    {
    for (int i=0;i<m_nPoolWid;++i)
    {
    byte  b1=m_pImageDateBuff[j*m_nPoolWid*4+i*4+0];
    byte  g1=m_pImageDateBuff[j*m_nPoolWid*4+i*4+1];
    byte  r1=m_pImageDateBuff[j*m_nPoolWid*4+i*4+2];
    b+=b1;
    g+=g1;
    r+=r1;
    
    }
    }
    int count=m_nPoolWid*m_nPoolHei;
    b/=count;
    g/=count;
    r/=count;
    }
    
    void CWaveDlg::AdjustContrastAndBrightness(void)
    {
    //out=avg+(in-avg)*(1+percent);avg=128;
        int avgB=0;
        int avgG=0;
    int avgR=0;
        AvgChannels(avgR,avgG,avgB);
    for (int j=0;j<m_nPoolHei;++j)
    for (int i=0;i<m_nPoolWid;++i)
    {
               byte  b=m_pImageDateBuff[j*m_nPoolWid*4+i*4+0];
       byte  g=m_pImageDateBuff[j*m_nPoolWid*4+i*4+1];
       byte  r=m_pImageDateBuff[j*m_nPoolWid*4+i*4+2];
               b=avgB+(int)((b-avgB)*(1+m_dbPercent));
       g=avgG+(int)((g-avgG)*(1+m_dbPercent));
       r=avgR+(int)((r-avgR)*(1+m_dbPercent));
       LimitMaxMin(b);
       LimitMaxMin(g);
               LimitMaxMin(r);
    
               m_pImageDateBuff1[j*m_nPoolWid*4+i*4+0]=b;
       m_pImageDateBuff1[j*m_nPoolWid*4+i*4+1]=g;
       m_pImageDateBuff1[j*m_nPoolWid*4+i*4+2]=r;
    }
    }
    RefreshImag();
    }
    
    void  CWaveDlg::AdjustSimpleContrastAndBrightness(void)
    {
    int index=0;
    int index1=0;
    int index2=0;
    int index3=0;
    for (int j=0;j<m_nPoolHei;++j)
    {
    for (int i=0;i<m_nPoolWid;++i)
    {
    index=j*(m_nPoolWid<<2)+(i<<2);
    index1=index+0;
                index2=index+1;
    index3=index+2;
    
    byte  b=m_pImageDateBuff[index1];
    byte  g=m_pImageDateBuff[index2];
    byte  r=m_pImageDateBuff[index3];
    
    b=b*m_dbPercent+m_bBrightness;
    g=g*m_dbPercent+m_bBrightness;
    r=r*m_dbPercent+m_bBrightness;
    LimitMaxMin(b);
    LimitMaxMin(g);
    LimitMaxMin(r);
    
    m_pImageDateBuff1[index1]=b;
    m_pImageDateBuff1[index2]=g;
    m_pImageDateBuff1[index3]=r;
    
    }
    }
    RefreshImag();
    }
    
    void CWaveDlg::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar) 
    {
    // TODO: Add your message handler code here and/or call default
    CSliderCtrl *pSlider=(CSliderCtrl *)pScrollBar;
    if (pSlider->GetDlgCtrlID()==IDC_SLIDER)
    {
            m_dbPercent=pSlider->GetPos()*0.01;
    
    }
    else if (pSlider->GetDlgCtrlID()==IDC_SLIDER2)
    {
            m_bBrightness=pSlider->GetPos();
    }
    //OnMenuitemBinary();
    AdjustSimpleContrastAndBrightness();
    CDialog::OnHScroll(nSBCode, nPos, pScrollBar);
    }
    
    void CWaveDlg::Rgb2Gray24(int typeFlag)
    {
       if (typeFlag==0)
       {
       for (int j=0;j<m_nPoolHei;++j)
       {
       for (int i=0;i<m_nPoolWid;++i)
       {
       byte  b=m_pImageDateBuff[j*m_nPoolWid*4+i*4+0];
       m_pImageDateBuff1[j*m_nPoolWid*4+i*4+0]=b;
                   m_pImageDateBuff1[j*m_nPoolWid*4+i*4+1]=b;
                   m_pImageDateBuff1[j*m_nPoolWid*4+i*4+2]=b;
       //byte  g=m_pImageDateBuff[j*m_nPoolWid*4+i*4+1];
           //byte  r=m_pImageDateBuff[j*m_nPoolWid*4+i*4+2];	   
       }
       }
       }
    
       else if(typeFlag==1)
    
       {
       for (int j=0;j<m_nPoolHei;++j)
       {
       for (int i=0;i<m_nPoolWid;++i)
       {
       
       byte  b=m_pImageDateBuff[j*m_nPoolWid*4+i*4+0];
       byte  g=m_pImageDateBuff[j*m_nPoolWid*4+i*4+1];
       byte  r=m_pImageDateBuff[j*m_nPoolWid*4+i*4+2];
       byte avg=(b+g+r)/3;
    
       m_pImageDateBuff1[j*m_nPoolWid*4+i*4+0]=avg;
                   m_pImageDateBuff1[j*m_nPoolWid*4+i*4+1]=avg;
                   m_pImageDateBuff1[j*m_nPoolWid*4+i*4+2]=avg;
       
       }
       }
       }
    
       else if (typeFlag==2)
       {
       for (int j=0;j<m_nPoolHei;++j)
       {
       for (int i=0;i<m_nPoolWid;++i)
       {
       
       byte  b=m_pImageDateBuff[j*m_nPoolWid*4+i*4+0];
       byte  g=m_pImageDateBuff[j*m_nPoolWid*4+i*4+1];
       byte  r=m_pImageDateBuff[j*m_nPoolWid*4+i*4+2];
       //byte avg=(b*11+g*59+r*30+50)/100;
       byte gray=(r+b+((USHORT)g<<1))>>2;
       m_pImageDateBuff1[j*m_nPoolWid*4+i*4+0]=gray;
                   m_pImageDateBuff1[j*m_nPoolWid*4+i*4+1]=gray;
                   m_pImageDateBuff1[j*m_nPoolWid*4+i*4+2]=gray;
       
       }
       }
       }
    }
    
    void CWaveDlg::OnMenuitemRgbToGray() 
    {
    // TODO: Add your command handler code here
    Rgb2Gray24(2);
    RefreshImag();
    }
    
    void CWaveDlg::Gray2Binary( byte* imageBuffer,byte threshold)
    {
       for (int j=0;j<m_nPoolHei;++j)
       {
       for (int i=0;i<m_nPoolWid;++i)
       {
       
       byte  b=imageBuffer[j*m_nPoolWid*4+i*4+0];
                   if (b>=threshold)
                   {
       b=255;
                   }
       else
       {
                     b=0;
       }
       imageBuffer[j*m_nPoolWid*4+i*4+0]=b;
                   imageBuffer[j*m_nPoolWid*4+i*4+1]=b;
                   imageBuffer[j*m_nPoolWid*4+i*4+2]=b;
       
       }
       }
    }
    
    void CWaveDlg::OnMenuitemOrgImag() 
    {
    // TODO: Add your command handler code here
    memcpy(m_pImageDateBuff1,m_pImageDateBuff,m_nPoolWid*m_nPoolHei*4);
    m_newBmp.SetBitmapBits(m_nPoolWid*m_nPoolHei*4,m_pImageDateBuff);	
        CDC memDC;
    memDC.CreateCompatibleDC(m_pDC);
    CBitmap *pOldBmp=memDC.SelectObject(&m_newBmp);
    m_pDC->BitBlt(0,0,m_nPoolWid,m_nPoolHei,&memDC,0,0,SRCCOPY);
    memDC.SelectObject(pOldBmp);
    memDC.DeleteDC();
    }
    
    
    void CWaveDlg::SaveBmp(void)
    {
       BITMAPINFO bmpInfo;
       memset(&bmpInfo,0,sizeof(BITMAPINFO));
       BITMAPFILEHEADER bmpFileHeader;
       BITMAPINFOHEADER bmpInfoHeader;
       bmpInfoHeader.biSize=sizeof(BITMAPINFOHEADER);
       bmpInfoHeader.biBitCount=32;
       bmpInfoHeader.biWidth=m_nPoolWid;
       bmpInfoHeader.biHeight=m_nPoolHei;
       bmpInfoHeader.biPlanes=1;
       bmpInfoHeader.biClrImportant=0;
    
       bmpInfoHeader.biClrUsed=0;
       bmpInfoHeader.biSizeImage=0;
       bmpInfoHeader.biXPelsPerMeter=0;
       bmpInfoHeader.biYPelsPerMeter=0;
       bmpInfoHeader.biCompression=BI_RGB;
    
    
       bmpFileHeader.bfType=0x4d42;
       bmpFileHeader.bfOffBits=sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER);
       bmpFileHeader.bfSize=bmpFileHeader.bfOffBits+m_nPoolHei*m_nPoolWid*4;
       bmpFileHeader.bfReserved1=0;
       bmpFileHeader.bfReserved2=0;
       
       bmpInfo.bmiHeader=bmpInfoHeader;
    
       CFile file;
       file.Open("F:\\work\\Wave\\test.bmp",CFile::modeCreate|CFile::modeWrite);
       file.Write(&bmpFileHeader,sizeof(BITMAPFILEHEADER));
       file.Write(&bmpInfo,sizeof(BITMAPINFO));
       file.Write(m_pImageDateBuff1,m_nPoolWid*m_nPoolHei*4);
    
       file.Close();
    
    }
    
     void CWaveDlg::SaveBmpSingle(void)
    {
    
     byte *pBmpBuffer=new byte[sizeof(BITMAPINFOHEADER)+sizeof(RGBQUAD)*256];
     BITMAPINFO *pbmpInfo=(BITMAPINFO*)(pBmpBuffer);
     //memset(&bmpInfo,0,sizeof(BITMAPINFO));
         for (int k=0;k<256;k++)
         {
            pbmpInfo->bmiColors[k].rgbBlue=k;
    pbmpInfo->bmiColors[k].rgbGreen=k;
    pbmpInfo->bmiColors[k].rgbRed=k;
    pbmpInfo->bmiColors[k].rgbReserved=0;
         }
    
     BITMAPFILEHEADER bmpFileHeader;
     BITMAPINFOHEADER bmpInfoHeader;
     bmpInfoHeader.biSize=sizeof(BITMAPINFOHEADER);
     bmpInfoHeader.biBitCount=8;
     bmpInfoHeader.biWidth=m_nPoolWid;
     bmpInfoHeader.biHeight=m_nPoolHei;
     bmpInfoHeader.biPlanes=1;
     bmpInfoHeader.biClrImportant=0;
     bmpInfoHeader.biClrUsed=0;
     bmpInfoHeader.biSizeImage=0;
     bmpInfoHeader.biXPelsPerMeter=0;
     bmpInfoHeader.biYPelsPerMeter=0;
     bmpInfoHeader.biCompression=BI_RGB;
    
     bmpFileHeader.bfType=0x4d42;
     bmpFileHeader.bfOffBits=sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)+sizeof(RGBQUAD)*256;
     bmpFileHeader.bfSize=bmpFileHeader.bfOffBits+m_nPoolHei*m_nPoolWid;
     bmpFileHeader.bfReserved1=0;
     bmpFileHeader.bfReserved2=0;
    
     pbmpInfo->bmiHeader=bmpInfoHeader;
    
    
     CFile file;
     file.Open("F:\\work\\Wave\\test.bmp",CFile::modeCreate|CFile::modeWrite);
     file.Write(&bmpFileHeader,sizeof(BITMAPFILEHEADER));
     file.Write(pBmpBuffer,sizeof(BITMAPINFOHEADER)+sizeof(RGBQUAD)*256);
    
     Rgb2Gray24(2);
         byte *pRgbIndex=new byte [m_nPoolWid*m_nPoolHei];
    
     for (int j=0;j<m_nPoolHei;++j)
     {
     for (int i=0;i<m_nPoolWid;++i)
     {
     
     byte  b=m_pImageDateBuff1[j*m_nPoolWid*4+i*4+0];
    
     pRgbIndex[j*m_nPoolWid+i]=b;
     }
       }
    
     file.Write(pRgbIndex,m_nPoolWid*m_nPoolHei);
     
         file.Close();
     delete[]pRgbIndex;
     delete[] pBmpBuffer;
    }
    
    void CWaveDlg::OnMenuitemBinary() 
    {
    // TODO: Add your command handler code here
    Rgb2Gray24(2);
        Gray2Binary(m_pImageDateBuff1,m_bBrightness);
    RefreshImag();
    }
    
    
    void CWaveDlg::InvertBuffer(INT direction)
    {
    if (direction==1)//x direction
    {
    
    int midWidth=m_nPoolWid/2;
    int temp=0;
    INT *pImageBuffer=(INT*)m_pImageDateBuff1;
    for (int j=0;j<m_nPoolHei;++j)
    {
    for (int i=0;i<midWidth;++i)
    {
    temp= pImageBuffer[j*m_nPoolWid+i];
    pImageBuffer[j*m_nPoolWid+i]=pImageBuffer[j*m_nPoolWid+m_nPoolWid-i-1];
    pImageBuffer[j*m_nPoolWid+m_nPoolWid-i-1]  =temp; 
    }
       }
    }
    
    
    else if (direction==2)// y direction
    {
    int temp=0;
    int midHeight=m_nPoolHei/2;
    INT *pImageBuffer=(INT*)m_pImageDateBuff1;
    for (int j=0;j<midHeight;++j)
    {
    for (int i=0;i<m_nPoolWid;++i)
    {
    temp= pImageBuffer[j*m_nPoolWid+i];
    pImageBuffer[j*m_nPoolWid+i]=pImageBuffer[(m_nPoolHei-1-j)*m_nPoolWid+i];
    pImageBuffer[(m_nPoolHei-1-j)*m_nPoolWid+i]  =temp; 
    }
       }
    }
    
    }
    
    void CWaveDlg::OnButtonScrollWnd() 
    {
    // TODO: Add your control notification handler code here
    //     m_nCount+=5;
    // 	CRect rectClient;
    // 	GetClientRect(&rectClient);
    // 	::ScrollWindow(GetSafeHwnd(),0,m_nCount,&rectClient,NULL);
    }
    
    void CWaveDlg::OnMenuitemInvertY() 
    {
    // TODO: Add your command handler code here
    InvertBuffer(2);
    RefreshImag();
    //SaveBmp();
    }
    
    void CWaveDlg::OnMenuitemInvertX() 
    {
    // TODO: Add your command handler code here
    InvertBuffer(1);
    RefreshImag();
    }
    
    void CWaveDlg::RoateImag(CPoint & roatePos,double roateAngle)
    {
    
    }
    
    
    void CWaveDlg::GetHistogram(int *Histo)
    {
       for (int j=0;j<m_nPoolHei;++j)
       {
       for (int i=0;i<m_nPoolWid;++i)
       {
       
       byte  b=m_pImageDateBuff1[j*m_nPoolWid*4+i*4+0];
                   Histo[b]++;
       }
       }
    
    }
    
    void CWaveDlg::OnMenuitemHisto() 
    {
    // TODO: Add your command handler code here
    INT histo[256];
    memset(histo,0,256*sizeof(INT));
    Rgb2Gray24(1);
    GetHistogram(histo);
    DrawHistoGram(histo);
    }
    
    void CWaveDlg::DrawHistoGram(int *Histo)
    {
        m_newBmp.SetBitmapBits(m_nPoolWid*m_nPoolHei*4,m_pImageDateBuff1);	
        CDC memDC;
    memDC.CreateCompatibleDC(m_pDC);
    CBitmap *pOldBmp=memDC.SelectObject(&m_newBmp);
        CPen pen(PS_SOLID,1,RGB(255,0,0));
    CPen* pOldPen=memDC.SelectObject(&pen);
    
    CPoint ptCord(50,m_nPoolHei-50);//坐标原点
        //画x轴
        memDC.MoveTo(ptCord.x,ptCord.y);
    memDC.LineTo(m_nPoolWid,ptCord.y);
    
    //画刻度
    //memDC.MoveTo(ptCord.x,ptCord.y);
        for (int x=ptCord.x;x<ptCord.x+256;x++)
        {
           memDC.MoveTo(x,ptCord.y);
       memDC.LineTo(x,ptCord.y-Histo[x-ptCord.x]/15);
        }
    
    //画y轴
        memDC.MoveTo(ptCord.x,ptCord.y);
    memDC.LineTo(ptCord.x,0);
    
    m_pDC->BitBlt(0,0,m_nPoolWid,m_nPoolHei,&memDC,0,0,SRCCOPY);
    memDC.SelectObject(pOldBmp);
    memDC.SelectObject(&pOldPen);
    memDC.DeleteDC();
    
    }
    void CWaveDlg::Rgb2Gray8(void)
    {
    
    
    }
    
    
    void CWaveDlg::Rgb2Gray16(void)
    {
    
    
    }
    void CWaveDlg::Rgb2Rgb8(void)
    {
    
    
    }
    void CWaveDlg::Rgb2Rgb16(void)
    {
    
    
    }
    
    void CWaveDlg::OnMenuitemSaveGray8bit() 
    {
    // TODO: Add your command handler code here
    SaveBmpSingle();
    }
    
    void CWaveDlg::SaveBmpBinary(void)
    {
    
    byte *pBmpBuffer=new byte[sizeof(BITMAPINFOHEADER)+sizeof(RGBQUAD)*2];
    BITMAPINFO *pbmpInfo=(BITMAPINFO*)(pBmpBuffer);
    //memset(&bmpInfo,0,sizeof(BITMAPINFO));
    for (int k=0;k<2;k++)
    {
            pbmpInfo->bmiColors[k].rgbBlue=k*255;
    pbmpInfo->bmiColors[k].rgbGreen=k*255;
    pbmpInfo->bmiColors[k].rgbRed=k*255;
    pbmpInfo->bmiColors[k].rgbReserved=0;
    }
    
    BITMAPFILEHEADER bmpFileHeader;
    BITMAPINFOHEADER bmpInfoHeader;
    bmpInfoHeader.biSize=sizeof(BITMAPINFOHEADER);
    bmpInfoHeader.biBitCount=1;
    bmpInfoHeader.biWidth=m_nPoolWid;
    bmpInfoHeader.biHeight=-m_nPoolHei;
    bmpInfoHeader.biPlanes=1;
    bmpInfoHeader.biClrImportant=0;
    bmpInfoHeader.biClrUsed=0;
    bmpInfoHeader.biSizeImage=0;
    bmpInfoHeader.biXPelsPerMeter=0;
    bmpInfoHeader.biYPelsPerMeter=0;
    bmpInfoHeader.biCompression=BI_RGB;
    
    bmpFileHeader.bfType=0x4d42;
    bmpFileHeader.bfOffBits=sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)+sizeof(RGBQUAD)*2;
    bmpFileHeader.bfSize=bmpFileHeader.bfOffBits+m_nPoolHei*m_nPoolWid;
    bmpFileHeader.bfReserved1=0;
    bmpFileHeader.bfReserved2=0;
    
    pbmpInfo->bmiHeader=bmpInfoHeader;
    
    CFile file;
    file.Open("F:\\work\\Wave\\test.bmp",CFile::modeCreate|CFile::modeWrite);
    file.Write(&bmpFileHeader,sizeof(BITMAPFILEHEADER));
    file.Write(pBmpBuffer,sizeof(BITMAPINFOHEADER)+sizeof(RGBQUAD)*2);	
    Rgb2Gray24(2);
    byte shiftX=0x01;
    
    for (int j=0;j<m_nPoolHei;++j)
    {
    for (int i=0;i<m_nPoolWid;++i)
    {
    
    byte  b=m_pImageDateBuff1[j*m_nPoolWid*4+i*4+0];
    int k=(j*m_nPoolWid+i+1)%8;
    
    
    }
      }
    
    // 	file.Write(pRgbIndex,m_nPoolWid*m_nPoolHei/8);
    
    file.Close();
    delete[] pBmpBuffer;
    
    }
    
    void CWaveDlg::OnMenuitemSaveBinaryBmp1bit() 
    {
    // TODO: Add your command handler code here
    SaveBmpBinary();
    }
    
    void CWaveDlg::OnMenuitemGlobGrayStretch() 
    {
    // TODO: Add your command handler code here
    GlobGrayStrench();
    }
    
    void CWaveDlg::HistogramEqualization(void)
    {
    INT histo[256];
    double pHisto[256];
    memset(histo,0,256*sizeof(INT));
    memset(pHisto,0,256*sizeof(double));
    Rgb2Gray24(1);
    GetHistogram(histo);
    int count=m_nPoolHei*m_nPoolWid;
        for (int ii=0;ii<256;++ii)
        {
          pHisto[ii]=histo[ii]/(double)count;
        }
       CalculateCDF(pHisto);
    
       for (int j=0;j<m_nPoolHei;++j)
       {
       for (int i=0;i<m_nPoolWid;++i)
       {
       
       byte  b=m_pImageDateBuff[j*m_nPoolWid*4+i*4+0];
               byte bValue=(byte)(pHisto[b]*255.0);
       m_pImageDateBuff1[j*m_nPoolWid*4+i*4+0]=bValue;
       m_pImageDateBuff1[j*m_nPoolWid*4+i*4+1]=bValue;
       m_pImageDateBuff1[j*m_nPoolWid*4+i*4+2]=bValue;	   
       }
    }
    
       GetHistogram(histo);
       DrawHistoGram(histo);
    }
    
    void CWaveDlg::CalculateCDF(double *p)
    {
       for (int i=0;i<256;++i)
       {
         for (int k=0;k<=i;++k)
         {
     p[i]+=p[k];
         }
       }
    }
    
    
    void CWaveDlg::GlobGrayStrench()
    {
    INT histo[256];
    memset(histo,0,256*sizeof(INT));
    Rgb2Gray24(1);
       byte max=0,min=0;
       INT j;
       for ( j=0;j<m_nPoolHei;++j)
       {
       for (int i=0;i<m_nPoolWid;++i)
       {
       
       byte  b=m_pImageDateBuff1[j*m_nPoolWid*4+i*4+0];	
       if (b>=max)
       {
       max=b;
       }
       if (b<=min)
       {
                 min=b;
       }
       }
    }
    
    
       byte d=max-min;
    
       for ( j=0;j<m_nPoolHei;++j)
       {
       for (int i=0;i<m_nPoolWid;++i)
       {
       
       byte  &b=m_pImageDateBuff1[j*m_nPoolWid*4+i*4+0];
       byte  &g=m_pImageDateBuff1[j*m_nPoolWid*4+i*4+1];
       byte  &r=m_pImageDateBuff1[j*m_nPoolWid*4+i*4+2];
       b=(byte)((b-min)*(255.0/d));
       g=(byte)((g-min)*(255.0/d));
       r=(byte)((r-min)*(255.0/d));
       }
    }
    
        GetHistogram(histo);
     	DrawHistoGram(histo);
    }
    
    void CWaveDlg::OnMenuitemHistoEqualization() 
    {
    // TODO: Add your command handler code here
    HistogramEqualization();
    }
    

    转载于:https://my.oschina.net/u/221120/blog/666332

    展开全文
  • 图像灰度直方图灰度直方图是图像灰度级的函数,用来描述每个灰度级在图像矩阵中的像素个数或者占有率。直方图显示图像数据时会以左暗右亮的分布曲线形式呈现出来。横坐标是灰度级,纵坐标是该灰度级出现的频率。图像...

    图像灰度直方图

    灰度直方图是图像灰度级的函数,用来描述每个灰度级在图像矩阵中的像素个数或者占有率。直方图显示图像数据时会以左暗右亮的分布曲线形式呈现出来。横坐标是灰度级,纵坐标是该灰度级出现的频率。图像的对比度是通过灰度级范围来度量的,而灰度级范围可通过观察灰度直方图得到,灰度级范围越大代表对比度越高;反之对比度越低,低对比度的图像在视觉上给人的感觉是看起来不够清晰,所以通过算法调整图像的灰度值,从而调整图像的对比度是有必要的。

    获得图像的灰度直方图def calGrayHist(img):

    h,w = img.shape[:2]

    gray_hist = np.zeros([256], np.uint64)

    for i in range(h):

    for j in range(w):

    gray_hist[img[i][j]] += 1

    return gray_hist

    不过通常会将纵坐标归一化到[0,1][0,1]区间内,也就是将灰度级出现的频率(像素个数)除以图像中像素的总数,得到归一化灰度直方图,计算公式如下:

    其中,

    是像素的灰度级,

    是具有灰度

    的像素的个数,MN是图像中总的像素个数。

    直方图均衡化 Histogram Equalization

    假如图像的灰度分布不均匀,其灰度分布集中在较窄的范围内,使图像的细节不够清晰,对比度较低。通常采用直方图均衡化及直方图规定化两种变换,使图像的灰度范围拉开或使灰度均匀分布,从而增大反差,使图像细节清晰,以达到增强的目的。

    直方图均衡化,对图像进行非线性拉伸,重新分配图像的灰度值,使一定范围内图像的灰度值大致相等。这样,原来直方图中间的峰值部分对比度得到增强,而两侧的谷底部分对比度降低,输出图像的直方图是一个较为平坦的直方图。

    直方图的均衡化实际也是一种灰度的变换过程,通常均衡化选择的变换函数是灰度的累积概率

    全局直方图均衡化def equalizeHist_map(img_gray):#输入必须是灰度图像

    h, w = img_gray.shape

    #计算灰度直方图

    gray_hist = calGrayHist(img_gray)

    #计算累加灰度直方图

    cumulative_hist = np.zeros([256], np.uint64)

    for p in range(256):

    if p == 0:

    cumulative_hist[p] = gray_hist[p]

    else:

    cumulative_hist[p] = cumulative_hist[p-1] + gray_hist[p]

    #根据累加灰度直方图得到输入灰度级和输出灰度级之间的映射关系

    relative_map = np.zeros([256], np.uint8)

    coff = 256.0/(h*w)

    for p in range(256):

    q = coff * float(cumulative_hist[p]) -1

    if q < 0:

    relative_map[p] = 0

    else:

    relative_map[p] = math.floor(q)

    return relative_map

    img = cv2.imread('test.png')

    img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

    relative_map = equalizeHist_map(img_gray)

    img_result = np.zeros(img.shape, np.uint8)

    h,w = img.shape[:2]

    for i in range(h):

    for j in range(w):

    img_result[i][j] = relative_map[img[i][j]]

    虽然全局直方图均衡化方法对提高对比度很有效,但是均衡化处理以后暗区域的噪声可能会被放大,变得清晰可见,而亮区域可能会损失信息。为了解决该问题,提出了自适应直方图均衡化(Aptive Histogram Equalization)方法,自适应直方图均衡化首先将图像划分为不重叠的区域块,然后对每一个块分别进行直方图均衡化。

    线性变换图像增强的缺点:需要根据不同的应用及图像自身的信息进行合理的选择,可能需要进行多次测试,所以选择合适的参数是相当麻烦的。有时候,需要图像具有某一特定的直方图形状(也就是灰度分布),而不是均匀分布的直方图。直方图正规化也叫做直方图匹配,用于将图像变换为某一特定的灰度分布,也就是其目的的灰度直方图是已知的。这其实和均衡化很类似,均衡化后的灰度直方图也是已知的,是一个均匀分布的直方图;而规定化后的直方图可以随意的指定,直方图正规化就是基于当前图像情况自动选取a和b的值的方法。

    ————————————————

    部分参考自:https://blog.csdn.net/victory_tc/article/details/97361911和https://www.cnblogs.com/wangguchangqing/p/7098213.html

    展开全文
  • Python实战社群Java实战社群长按识别下方二维码,按需求添加扫码关注添加客服进Python社群▲扫码关注添加客服进Java社群▲作者丨老肥来源丨老肥码码码(algo_and_da...
  • python2%线性拉伸

    2021-01-20 02:20:50
    核心要点 本方法是针对遥感数据的,因此使用了Gdal,如果你针对的是普通的图片,就直接使用cv2.imread()来读取即可,就不需要gdal了。 np.percentile()函数 np.clip()函数 cv2.split()函数 cv2.merge()函数 ...
  • 图像灰度直方图灰度直方图是图像灰度级的函数,用来描述每个灰度级在图像矩阵中的像素个数或者占有率。直方图显示图像数据时会以左暗右亮的分布曲线形式呈现出来。横坐标是灰度级,纵坐标是该灰度级出现的频率。图像...
  • 灰度拉伸

    2014-03-05 22:43:00
    2019独角兽企业重金招聘Python工程师标准>>> ...
  • ·对于部分图像,会出现整体较暗或较亮的情况,这是由于图片的灰度值范围较小,即对比度低。实际应用中,通过绘制图片的灰度直方图,可以很明显的判断图片的灰度值分布,区分其对比度高低。对于对比度较低的图片,...
  • # 图像拉伸 minimum = img.min() maximum = img.max() a = 255/(maximum-minimum) b = -255*minimum/(maximum-minimum) return (a*img+b).astype(np.uint8) if __name__ == &#...
  • 对比度拉伸是扩展图像灰度级动态范围的处理。通过在灰度级中确定两个点来控制变换函数的形状。下面是对比度拉伸函数中阈值处理的代码示例,阈值为平均值。 2. 测试结果 图源自skimage 3. 代码 1 def ...
  • 灰度直方图拉伸

    2021-07-22 17:07:01
    本项目编程环境:python3.8 读入彩色图像,转化为灰度图像,对该图像进行直方图统计,对灰度处于 15%和最高 15% 直方柱进行灰度直方图均衡。 图1 原图 1)先对其进行彩色直方图统计。 import matplotlib....
  • opencv python图像的灰度变换

    千次阅读 2018-08-28 11:18:23
    python中opencv图像处理实验(一)—灰度变换 ...
  • Python由图片生成三维模型。大致流程是:图片转换成灰度图,按照灰度值大小将像素点拉伸相应的距离,生成STL格式三维模型,用于雕刻或者3D打印。 想用Python实现,能说一下大致思路吗?尤其是如何生成三维模型。
  • 图像灰度的非线性变换平方变换对数变换 平方变换 将灰度进行平方再除以255,用于 降级图像亮度 增强亮部对比度 减缓暗部对比度 对比度可以看作灰度的斜率 对数变换
  • 拉伸低对比度图像、过度曝光图像,观察图像变换,对图像直方图均衡算法一、在开始之前知识点二、开始三、灰度拉伸,观察它们的图像变化、直方图变化1、先对 一副低对比度图像 灰度拉伸2、观察图像变化、直方图变化3...
  • 灰度图像的拉伸

    2017-03-21 19:31:00
    2019独角兽企业重金招聘Python工程师标准>>> ...
  • stretch(src) # 调用自定义函数对灰度图g03进行直方图拉伸 cv.imshow('src', src) # 原图显示 cv.imshow('result', result) # 直方图拉伸结果显示 cv.waitKey() # 无限刷新图像 cv.destroyAllWindows() # 关闭所有...
  • def stretch_4(img): """ constrast stretch, $(r_1, s_1) = (r_{min}, 0), (r_2, s_2) = (r_{max}, L-1)$ return image stretch use loop can get right image, but numpy still work, but the return image is...

空空如也

空空如也

1 2 3 4 5 ... 20
收藏数 1,167
精华内容 466
关键字:

灰度拉伸python

python 订阅