In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Most people do not understand the knowledge points of this article "how to use PyQt5 to embed Matplotlib images into Scoll Area and display scrollbar effects", so the editor summarizes the following contents, detailed contents, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this article entitled "how to embed Matplotlib images in Scoll Area and display scroll bar effects using the PyQt5 implementation."
Resolve step 1 qt designer
When designing a ui file, the location of the control needs to be the same as that of the control in your own project, so that it is convenient for the project to call after the function is implemented.
Save as testpiv.ui file
2 pycharm programming
Code
Import cv2import osimport sysimport mathfrom PyQt5 import QtCorefrom PyQt5.QtWidgets import * from PyQt5.uic import loadUiimport matplotlibfrom matplotlib import pyplot as pltmatplotlib.use ("Qt5Agg") # declares the use of QT5from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvasmatplotlib.use ("Qt5Agg") # declares the use of QT5from matplotlib.backends.backend_qt5agg import NavigationToolbar2QT as NavigationToolbar# to create a matplotlib graphics class class MyFigure (FigureCanvas): def _ _ init__ (self,width, height, dpi): # create a Figure The Figure is the Figure under matplotlib It is not Figure self.fig = plt.figure (figsize= (width, height), dpi=dpi) # under matplotlib.pyplot to activate the Figure window in the parent class, this sentence is necessary, otherwise the graphic super (MyFigure,self). _ init__ (self.fig) # calls the add_subplot method under Figure Similar to the subplot method class scollarea_showpic (QMainWindow) below matplotlib.pyplot: def _ _ init__ (self, queryPath=None, samplePath=None) Limit_value = None): super (). _ init__ () self.queryPath = queryPath # Gallery path self.samplePath = samplePath # sample picture self.limit_value = limit_value self.ui () plt.rcParams ['font.sans-serif'] = [' KaiTi'] # only in this way can Chinese fonts display def ui (self) : loadUi ('. / testpiv.ui' Self) self.SIFT (self.queryPath,self.samplePath,self.limit_value) def getMatchNum (self,matches,ratio):''return the number of feature point matches and matching mask' 'matchesMask= [[0Let0] for i in range (len (matches))] matchNum=0 for I, (m N) in enumerate (matches): if m.distance < ratio * n.distance: # delete and select matching points whose distance ratio is less than ratio matchesMask [I] = [1Magne 0] matchNum+=1 return (matchNum,matchesMask) def SIFT (self,dirpath,picpath) Limit_value): # path='F:/python/gradu_design/gra_des/' queryPath=dirpath # Gallery path samplePath=picpath # sample image comparisonImageList= [] # record comparison result # create SIFT feature extractor sift = cv2.xfeatures2d.SIFT_create () # create FLANN matching object "" FLANN is similar to the nearest neighbor It will choose the most appropriate algorithm based on the data itself to process the data 10 times faster than other search algorithms "" FLANN_INDEX_KDTREE=0 indexParams=dict (algorithm=FLANN_INDEX_KDTREE) Trees=5) searchParams=dict (checks=50) flann=cv2.FlannBasedMatcher (indexParams,searchParams) sampleImage=cv2.imread (samplePath,0) kp1, des1 = sift.detectAndCompute (sampleImage, None) # extract features for parent,dirnames,filenames in os.walk (queryPath): print ('parent:', parent,'', 'dirnames:' Dirnames) for p in filenames: p=queryPath+p # print ('pic filename:', p) queryImage=cv2.imread (pjingo) kp2, des2 = sift.detectAndCompute (queryImage, None) # extract features matches=flann.knnMatch (des1,des2,k=2) # match feature points In order to delete matching points, specify k as 2, so that for each feature point of the sample graph, two matches (matchNum,matchesMask) = self.getMatchNum (matches,0.9) # pass the ratio condition Calculate the matching degree of matchRatio=matchNum*100/len (matches) drawParams=dict (matchColor=), singlePointColor= (255 flags=0 0), comparisonImage=cv2.drawMatchesKnn (sampleImage,kp1,queryImage,kp2,matches,None,**drawParams) comparisonImageList.append ((comparisonImage) MatchRatio) # record the result comparisonImageList.sort (key=lambda XVX [1], reverse=True) # sort by matching degree descending new_comparisonImageList = comparisonImageList [: limit_value] count=len (new_comparisonImageList) column = 1 # column row = math.ceil (count/column) # row math.ceil: the function returns the smallest integer print ('column:', column) that is greater than or equal to a given number The drawing shows F = MyFigure (width=10, height=10, dpi=100) # 500x400 for index, (image,ratio) in enumerate (new_comparisonImageList): F.axes = F.fig.add_subplot (row,column) Index+1) F.axes.set_title ('Similiarity% .2f%'% ratio) plt.imshow (image) # resize the gap between subplot plt.subplots_adjust (hspace=0.2) self.figure = F.fig # FigureCanvas: canvas self.canvas = FigureCanvas (self.figure) # fig has canvas self.canvas.resize (self.picwidget.width () 3000) # canvas size self.scrollArea = QScrollArea (self.picwidget) # scroll self.scrollArea.setFixedSize (self.picwidget.width (), self.picwidget.height ()) self.scrollArea.setWidget (self.canvas) # widget has scroll scroll and canvas self.nav = NavigationToolbar (self.canvas, self.picwidget) # create toolbar self.setMinimumSize (self.width () Self.height () self.setMaximumSize (self.width (), self.height ()) self.setWindowTitle ('Test') if _ _ name__ = "_ _ main__": app = QApplication (sys.argv) queryPath='F:/python/gradu_design/gra_des/imges/' # gallery path samplePath='F:/python/gradu_design/gra_des/imges/resized_logo1_1.jpg' # sample picture main = scollarea_showpic (queryPath SamplePath,3) main.show () sys.exit (app.exec_ ()) above is the content of this article on "how to use PyQt5 to embed Matplotlib images in Scoll Area and display scroll bar effects" I believe we all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please pay attention to the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.